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');
}
}