我正在尝试使用嵌套关系上的嵌套行详细信息查看laravel数据表。以下是我所做的细节
数据库关系
我希望数据表在页面加载时查看所有产品,然后第一级行详细信息将显示产品的所有组件。第二级行详细信息将显示组件中的所有子组件。
这是我的数据表视图的当前结果
如图所示,数据表无法显示子组件的原始材料名称。 以下是我的雄辩模型和关系。
产品型号
protected $table = 'products';
public function components() {
return $this->hasMany('App\Component', 'product_id', 'id');
}
组件模型
protected $table = 'components';
public function comp_subcomps() {
/*The syntax ->with('subcomponents') seems WORKING fine in resulting
the second level row detail ( viewing the subcomponents of a component )*/
return $this->hasMany('App\CompToSubComp', 'component_id', 'id')->with('subcomponents');
}
CompToSubComp模型
protected $table = 'comp_subcomp';
public function subcomponents() {
/* This syntax ->with('rawmaterials') seems NOT WORKING to
get the material name in the second level row detail */
return $this->belongsTo('App\SubComponent', 'subcomponent_id', 'id')->with('rawmaterials');
}
子组件模型
protected $table = 'subcomponents';
public function rawmaterials() {
return $this->belongsTo('App\Rawmaterial', 'rawmaterial_id', 'id');
}
CONTROLLER
/* On page load */
public function viewTable() {
$products = Product::select();
return Datatables::of($products)
->addColumn('details_url', function($product) {
return url('/product/getdetaildata/' . $product->id);
})->make(true);
}
/* First level row detail ( showing components of a product ) */
public function getDetailsData($id)
{
$components = Product::find($id)->components();
return Datatables::of($components)
->addColumn('moredetails_url', function($component) {
return url('/product/getmoredetail/' . $component->id);
})->make(true);
}
/* Second level row detail ( showing sub-components of a component ) */
public function getMoreDetailsData($id) {
$compsubcomp = Component::find($id)->comp_subcomps();
return Datatables::of($compsubcomp )->make(true);
}
JS DATATABLE
/* On load: Its working */
var dt = $('#example').DataTable({
...
"columns": [
{ "data": "id", "name": "id" },
{ "data": "name", "name": "name"}
]
});
/* First level row detail: Its working */
var tabComponent = $('#' + tableId).DataTable({
...
"columns": [
{ "data": "id", "name": "id" },
{ "data": "name", "name": "name" }
]
});
/* Second level: This PROBLEM exists where RAWMATERIAL NAME is not showing */
$('#' + tableId).DataTable({
...
"columns": [
{ "data": "id", "name": "subcomponents.id" },
{ "data": "name", "name": "subcomponents.name" },
{ "data": "rawmaterial_id", "name": "subcomponents.rawmaterial_id" },
{ "data": "subcomponents.rawmaterials.name", "name": "subcomponents.rawmaterials.name" }
]
});
如何正确查看数据表中'rawmaterials'和'subcomponents'之间的关系?
任何帮助识别我的错误将不胜感激。提前谢谢。