我有 3 个表 - 收集、装运和包裹。
问题1
集合具有一对多货件。一个货件每个货件可能有一个到多个包裹。
Collections table
id
user_id
Shipments table
id
collections_id
shipment information(currently irrelevant)
Parcels table
id
shipment_id
parcelnumber
显示刀片
如何在边栏选项卡中显示它们,如下所示?
边栏选项卡视图
问题2
创建刀片
当用户添加新货件时,应创建一个货件,并将新创建的货件与该货件相关联。如何将新创建的货件分配给取件?
我真的需要关于我是否应该重新开始并更改数据库表构建的建议
如果需要有关型号/视图/控制器的任何其他信息,请告诉我。
集合模型
namespace App;
use IlluminateDatabaseEloquentModel;
class Collections extends Model
{
public function Shipments() {
return $this->hasMany('AppShipments');
}
public function Parcels() {
return $this->hasManyThrough('AppParcels','AppShipments');
}
}
亲切问候
用户3088327
在您的模型中尝试此解决方案
public function Shipments() {
return $this->hasOne(AppModelShipments:class);
}
public function Parcels() {
return $this->hasManyThrough(AppModelParcels:class,AppModelShipments:class);
}
在刀片中
$comments = AppModelCollection::find(1)->Shipments()->Parcels()->get();
foreach ($Shipments as $Shipment) {
//
}
foreach ($Parcels as $Parcel) {
//
}
使用另一种方法:("use Illuminate\Support\Facades\DB;"(
$combine = DB::table('collections')
->JOIN('shipments','collections.id,'=',shipments.collection_id')
->JOIN('parcels','parcels.shipments_id','=','shipments.id')
->SelectRaw('combines.id as chipID, ...')
->get();
或: 在型号(出货量(:
public function Shipments() {
return $this->belongsTo('Appcollections'); //can add ->select(..)
}
public function Parcels() {
return $this->hasMany('AppParcels');
}
在控制器中:
$collections= Shipments::with('shipments')->with('parcels')->get();