Answer the question
In order to leave comments, you need to log in
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:
<?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');
}
}
<?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');
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question