полиморфные отношения проблемы с караваном зрения - PullRequest
0 голосов
/ 06 ноября 2018

привет, я хочу использовать полиморфные отношения в моем приложении но это не работает правильно это моя модель 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');
    }  

}

и это моя статья Код контроллера

<?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, "creation_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 , "creat_at": "2018-11-06 16:10:42", "updated_at": "2018-11-06 16:10:42"}]

внутри столбца тегов вместо тега, я не вижу, в чем проблема

1 Ответ

0 голосов
/ 18 ноября 2018

проблема была в моем способе использования отношений я снова прочитал документацию и понял концепцию и знать, как правильно его использовать

...