D
D
Dmitry Kuznetsov2018-03-12 09:19:41
Laravel
Dmitry Kuznetsov, 2018-03-12 09:19:41

Why aren't tags displayed on polymorphic links in Laravel 5.6?

It was required to add polymorphic links on the site to get additional. blog data (my site is a blog).
What do I have:

DB
Tags :
5aa61b4e3bb03085738922.pngTaggables :
5aa61b52da6fb976282621.pngBlogs :
5aa61b5864514743680757.png
The code
blogs :
<?php
namespace App\Models\Blogs;

use App\User;
use Illuminate\Database\Eloquent\Model;

class Blogs extends Model{

    protected $table = 'blogs';
    protected $primaryKey = 'id';
    protected $fillable = [];
    protected $hidden = [];
    protected $casts = [];

    public static function getBlogs(int $paginate = 10){
        $blogs = Blogs::paginate($paginate);

        if($blogs){
            $blogs->load('author');
            $blogs->load('tags');
        }

        return $blogs;
    }

    /*
     *  Отношения
     */

    /**
     * Получение авторов блогов
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function author(){
        return $this->hasOne(User::class, 'id', 'author_blogs');
    }

    /**
     * Получение тэгов у блога
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
     */
    public function tags(){
        return $this->morphToMany(Tags::class, 'taggable');
    }
}

Tags :
<?php
namespace App\Models\Blogs;

use Illuminate\Database\Eloquent\Model;

class Tags extends Model{

    protected $table = 'tags';
    protected $primaryKey = 'id';
    protected $fillable = [];
    protected $hidden = [];
    protected $casts = [];

    /*
     * Отношения
     */

    /**
     * Полиморфное отношение блогов с тэгами
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
     */
    public function blogs(){
        return $this->morphedByMany(Blogs::class, 'taggable');
    }
}


And in the end, I get only blogs and their authors, but not the tags themselves. Tell me where to dig, please!)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
anlamas, 2018-03-12
@dima9595

Did you add it to the database manually? Because as I remember, Lara stores a class in taggable_type, like "App\Blog".
Maybe this is a mistake?
PS Your class represents an entity (one), so I recommend renaming it to Blog, Tag.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question