如何从eloquet查询中的数据透视表获取关系?

时间:2019-04-22 14:28:14

标签: laravel

我想获取数据透视模型的关系照片

print(chunks([1,2,3,4,5,6,7,8,9],3))
#[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(chunks([1,2,3,4,5,6,7,8,9,10],3))
#[[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]

调用Pivot模型的模型就是这样

class WeedHasState extends Pivot
{
    protected $table = 'weed_has_state';
    protected $fillable =['weed_id','weed_state_id','id'];

    public function weed()
    {
        return $this->belongsTo(Weed::class, 'weed_id');
    }

    public function state()
    {
        return $this->belongsTo(WeedState::class);
    }

    public function photos(){
        return $this->hasMany(WeedPhoto::class);
    }
}

打电话给class Weed extends BaseModel { public function states() { return $this->belongsToMany(WeedState::class, 'weed_has_state', 'weed_id', 'weed_state_id') ->using(WeedHasState::class) ->withPivot('weed_id', 'weed_state_id') ->withTimestamps(); } } 不起作用吗?

0 个答案:

没有答案