![](/img/trans.png)
[英]Table naming convention for Laravel Eloquent hasManyThrough relation
[英]Laravel Eloquent hasManyThrough relation + pagination
我有三個表:
Videos:
-id
-name
-another fields
Cats:
-id
-name
-another fields
Cats_videos:
-id
-cat_id
-video_id
和三種口才模型:
class cat extends Model
{
protected $table = 'cat';
public function AllCatVideos()
{
return $this->hasManyThrough('App\videos', 'App\cats_videos','cat_id','id');
}
}
class videos extends Model
{
public $timestamps = false;
protected $table ='videos';
}
class cats_videos extends Model
{
protected $table = 'cats_videos';
}
因此,我想用一只貓接收所有視頻,然后對結果進行分頁。 我試圖這樣做:
$cat = new Cats();
$videos = $cat->find(58)->AllCatVideos()->toSql();
我收到這樣的sql:
select * from `videos` inner join `cats_videos` on `cats_videos`.`id` = `videos`.`id` where `cats_videos`.`cat_id` = 58
但我需要“ cats_videos
。 video_id
= videos
。 id
”,而不是“ cats_videos
。 id
= videos
。 id
”。 如何使用“ hasManyThrough”執行此操作? 還是我應該看看別的東西?
問題解決了。 我只需要使用belongsToMany。
class cat extends Model{
public function Videos()
{
return $this->belongsToMany('App\videos','cats_videos','cat_id','video_id');
}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.