【发布时间】:2017-02-26 06:27:52
【问题描述】:
我有两张桌子
1- 项目 2- 客户
我需要从客户表中获取名称,从项目表中获取项目名称
我的项目类有
public function projectClient()
{
return $this->hasMany('User');
}
我的用户类有
class User extends Eloquent implements UserInterface, RemindableInterface
{
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'client';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
public function project()
{
return $this->belongsTo('Projects', 'id');
}
}
在我的客户控制器中
$clients = User::with('project')->get();
//dd($clients);
return View::make('admin.manageClients.viewClients', compact('clients'));
现在在视图中我试试这个
@foreach($clients as $clt)
{{ $clt->first_name }} //work fine
{{ $clt->pro_title }} //didn't show the project title
@endforeach
如何在此处显示此项目标题?
更新
@Mahfuzul Alam 和 @WebKenth 建议我这样做
{{ $clt->project->pro_title }}
但还是有问题
在我的数据库中,我有两个用户,project_id = 1
但在我的view 第一个用户project_id = 1 和第二个project_id = 2
【问题讨论】: