我有两张桌子。这些是客户和项目。每个客户都有许多项目,每个项目都与一个客户有关。我想输出视图模板中与每个客户端相关的所有项目。请帮我
public function client()
{
return $this->belongsTo('App\Model\Admin\Client');
}
public function projects()
{
return $this->hasMany('App\Model\Admin\Project');
}
我想在刀片模板中输出如下所示:
id -| Client_name |- client_company |- contact_number-|- project_list
----|-------------|-----------------|-----------------|-----------------
1 | x | [email protected] |xxxxxxxxxxxxxxx | first project
| | | | second project
| | | | Third project
----------------|-----------------|-----------------|--------------------
2 | y | [email protected] |xxxxxxxxxxxxxxx | first project
| | second project
| Third project
首先,您需要获得所有客户端的热切加载项目,如下例所示:
$ clients = Clients :: with('projects') - > get();
现在在视图模板中,您需要使用以下代码:
<?php
foreach($clients as $client) {
// Here you can get client details in $client object
//$client->name give you client name
foreach($client->projects as $project){
// Here you can get projects details in $project object
// $project->name give you project name
}
}
你可以通过这种方式做到这一点
return $this->belongsTo('App\User', 'foreign_key', 'other_key');
希望这可以帮助 :)