Php Laravel连接表以从每个表中获取属性
我有三张桌子:Php Laravel连接表以从每个表中获取属性,php,sql,laravel,eloquent,Php,Sql,Laravel,Eloquent,我有三张桌子: warehouses: id | warehouse_name 我正在尝试获取大量记录,并希望在视图中显示这些记录,如下所示: good_name | warehouse_name | quantity | 什么是正确的查询,以获得这个结果与雄辩 好的车型 class Good extends Model { /** * Get the Good Quantity for this Good. */ public function good_quantities(
warehouses: id | warehouse_name
我正在尝试获取大量记录,并希望在视图中显示这些记录,如下所示:
good_name | warehouse_name | quantity |
什么是正确的查询,以获得这个结果与雄辩
好的车型
class Good extends Model
{
/**
* Get the Good Quantity for this Good.
*/
public function good_quantities()
{
return $this->hasMany('App\Models\Good_Quantity','good_id');
}
}
class Warehouse extends Model
{
/**
* Get the Goods Quantity for this Warehouse.
*/
public function good_quantities()
{
return $this->hasMany('App\Models\Good_Quantity','warehouse_id');
}
}
class Good_Quantity extends Model
{
/**
* Get the Good's Info.
*/
public function good()
{
return $this->belongsTo('App\Models\Good','good_id');
}
/**
* Get the Good Quantity's Warehouse.
*/
public function warehouse()
{
return $this->belongsTo('App\Models\Warehouse','warehouse_id');
}
}
仓库模型
class Good extends Model
{
/**
* Get the Good Quantity for this Good.
*/
public function good_quantities()
{
return $this->hasMany('App\Models\Good_Quantity','good_id');
}
}
class Warehouse extends Model
{
/**
* Get the Goods Quantity for this Warehouse.
*/
public function good_quantities()
{
return $this->hasMany('App\Models\Good_Quantity','warehouse_id');
}
}
class Good_Quantity extends Model
{
/**
* Get the Good's Info.
*/
public function good()
{
return $this->belongsTo('App\Models\Good','good_id');
}
/**
* Get the Good Quantity's Warehouse.
*/
public function warehouse()
{
return $this->belongsTo('App\Models\Warehouse','warehouse_id');
}
}
好的数量模型
class Good extends Model
{
/**
* Get the Good Quantity for this Good.
*/
public function good_quantities()
{
return $this->hasMany('App\Models\Good_Quantity','good_id');
}
}
class Warehouse extends Model
{
/**
* Get the Goods Quantity for this Warehouse.
*/
public function good_quantities()
{
return $this->hasMany('App\Models\Good_Quantity','warehouse_id');
}
}
class Good_Quantity extends Model
{
/**
* Get the Good's Info.
*/
public function good()
{
return $this->belongsTo('App\Models\Good','good_id');
}
/**
* Get the Good Quantity's Warehouse.
*/
public function warehouse()
{
return $this->belongsTo('App\Models\Warehouse','warehouse_id');
}
}
首先,您可以将BelongToMany用于仓库和货物学习多对多关系: