我有两个桌子。这些是客户和项目。每个客户都有多个项目,每个项目都与一个客户相关。我想在与每个客户端相关的视图模板中输出所有项目。请帮助我
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 | x@.com |xxxxxxxxxxxxxxx | first project
| | | | second project
| | | | Third project
----------------|-----------------|-----------------|--------------------
2 | y | y@.com |xxxxxxxxxxxxxxx | first project
| | second project
| Third project
答案 0 :(得分:3)
首先,您需要让所有具有急切加载项目的客户端(例如 下面的示例:
$ 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
}
}
答案 1 :(得分:2)
您可以通过这种方式
return $this->belongsTo('App\User', 'foreign_key', 'other_key');
希望这会有所帮助:)