2 回答

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以為此使用 eloquent 關(guān)系。在您的項(xiàng)目模型中使用兩個(gè)關(guān)系,如下所示:
class Project extends Model
{
public function manager()
{
return $this->belongsTo(User::class, 'manager_id');
}
public function employee()
{
return $this->belongsTo(User::class, 'employee_id');
}
}
所以在視圖中它會(huì)是這樣的
Project Name : {{ $project->name }}
Managed By : {{ $project->manager->name }}
Conduct By : {{ $project->employee->name }}
在這里查看 Laravel Eloquent 關(guān)系

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊
你應(yīng)該添加
protected $table = 'project' 因?yàn)槟愕谋砻?single。每當(dāng)您創(chuàng)建用戶、項(xiàng)目、服務(wù)等表時(shí),Laravel 都需要一個(gè)“s”。在數(shù)據(jù)透視表名稱中,如 user_role 、 role_permission 等。
class Project extends Model{
protected $table = 'project';
public function manager() {
return $this->belongsTo(User::class, 'manager_id');
}
public function employee(){
return $this->belongsTo(User::class, 'employee_id');
}
}
對(duì)于 Show at Blade 文件。
// Controller
public function index(){
$projects = Project::all();
return view('blade_File_Destination_Here',compact('projects'));
}
在刀片文件中查看進(jìn)程
@foreach($projects as $project)
Project
-------------------------
Project Name : {{ $project->name }}
Manage By : {{ $project->manager->name }}
Conduct By : {{ $project->employee->name }}
-------------------------
@endforeach
希望它對(duì)你有用
- 2 回答
- 0 關(guān)注
- 204 瀏覽
添加回答
舉報(bào)