小编ATe*_*Guy的帖子

如何在laravel中获得第三个关系

我有以下模型:用户、设备、产品。

用户

  public function devices()
{
    return $this->hasMany('App\Device');
}
Run Code Online (Sandbox Code Playgroud)

设备

 public function user()
{

    return $this->BelongsTo('App\User');

}

public function reading()
{
    return $this->hasMany('App\Reading', 'device_id', 'part_id');
}

public function product()
{
    return $this->hasOne('App\Product');
}
Run Code Online (Sandbox Code Playgroud)

产品

 public function device()
{
    return $this->belongsTo('App\Device');
}
Run Code Online (Sandbox Code Playgroud)

以下查询会拉取我的用户及其所有设备,但在该集合中并不是从设备到产品的关系。

$deviceSingles = User::with('devices')->get();
Run Code Online (Sandbox Code Playgroud)

此查询为我获取分配给它的所有设备的所有产品

$deviceSinglesTwo = Product::with('device')->get();
Run Code Online (Sandbox Code Playgroud)

我如何获得第三个关系,附加到我的初始查询,以便我可以做到这一点

$deviceSingles->device->product->title
Run Code Online (Sandbox Code Playgroud)

php laravel

2
推荐指数
1
解决办法
498
查看次数

标签 统计

laravel ×1

php ×1