簡體   English   中英

Laravel Eloquent具有很多聯系和分頁

[英]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_videosvideo_id = videosid ”,而不是“ cats_videosid = videosid ”。 如何使用“ 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM