Laravel Eloquent hasManyThrough 关系 + 分页
Laravel Eloquent hasManyThrough relation + pagination
我有三个表:
Videos:
-id
-name
-another fields
Cats:
-id
-name
-another fields
Cats_videos:
-id
-cat_id
-video_id
和三个Eloquent模型:
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');
}}
我有三个表:
Videos:
-id
-name
-another fields
Cats:
-id
-name
-another fields
Cats_videos:
-id
-cat_id
-video_id
和三个Eloquent模型:
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');
}}