多态关系laravel问题的观点

时间:2018-11-06 14:18:49

标签: laravel laravel-blade polymorphic-associations

嗨,我想在我的应用中使用多态关系 但它不能正常工作 这是我的tagtab模型

namespace App\Models;

use App\Models\Root\Root;

class TagTab extends Root
{

    public function taggable()
    {
        return $this->morphTo();
    }

}

这是我的文章模型代码

<?php

namespace App\Models;

use App\Models\Root\Root;


class Article extends Root
{

  public function articletags()
    {
        return $this->morphMany(TagTab::class, 'taggable');
    }  

}

这是我的articleController代码

<?php

namespace App\Http\Controllers;

use App\Libs\Message;
use App\Models\Article;
use App\Models\Tag;
use App\Models\TagTab;
use Illuminate\Http\Request;
use App\Http\Controllers\TagTabController;
/**
 * Class ArticleController
 * @package App\Http\Controllers
 */
    class ArticleController extends Controller
{
      /**
     * @var Article
     */
    private $New;


    /**
     * ArticleController constructor.
     */
    public function __construct()
    {
        $this->New = new Article();
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $articles = $this->New->index($this->Model);
        return view('index', compact($articles));
    }

}

这是我的索引页代码

<!-- index.blade.php -->

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Index Page</title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
</head>
<body>
<div class="container">
    <br/>
    @if (\Session::has('success'))
        <div class="alert alert-success">
            <p>{{ \Session::get('success') }}</p>
        </div><br/>
    @endif
    <table class="table table-striped">
        <thead>
        <tr>
            <th>ID</th>
            <th>name</th>
            <th>tags</th>
            <th>created_at</th>
            <th>updated_at</th>
            <th colspan="2">Action</th>
        </tr>
        </thead>
        <tbody>

        @foreach($articles as $article)
                @if($article->is_deleted==0)
                    <tr>
                        <td>{{$article->id}}</td>
                        <td>{{$article->title}}</td>
                        <td>{{$article->tags}}</td>
                        <td>{{jdate($article->created_at)->format('datetime')}}</td>
                        <td>{{jdate($article->updated_at)->format('datetime')}}</td>
                        <td><a href="{{action('ArticleController@edit', $article->id)}}"
                               class="btn btn-warning">Edit</a></td>
                        <td>
                            <form action="{{action('ArticleController@destroy', $article->id)}}" method="post">
                                @csrf
                                <input name="_method" type="hidden" value="DELETE">
                                <button class="btn btn-danger" type="submit">Delete</button>
                            </form>
                        </td>
                    </tr>
                @endif
        @endforeach

        </tbody>
    </table>
</div>
</body>
</html>

但是当我加载索引页面时,我得到了这个

  

[{“ id”:4,“ taggable_type”:“ App \ Models \ Article”,“ taggable_id”:2,“ tag_id”:1,“ created_at”:“” 2018-11-06 16:10:42 “,” updated_at“:” 2018-11-06 16:10:42“},{” id“:5,” taggable_type“:” App \ Models \ Article“,” taggable_id“:2,” tag_id“:2 ,“ created_at”:“ 2018-11-06 16:10:42”,“ updated_at”:“ 2018-11-06 16:10:42”}]

在标签列而不是标签内,我看不到问题所在

1 个答案:

答案 0 :(得分:0)

问题是我使用这种关系的方式 我再次阅读了文档,使概念更好 并知道如何正确使用它