Laravel - 多个外键和获取 id
Laravel - Multiple foreign keys and getting id
我先尝试了很多不同的东西,但似乎没有得到结果。
控制器:
public function all()
{
$projects = Project::all();
foreach($projects as $project) {
$pid = $project->id;
}
$am = DB::table('projects')
->join('employees', 'projects.am_id', '=', 'employees.id')
->where('projects.id', '=', $pid)
->select('projects.id', 'projects.am_id', 'employees.name')
->first();
$pm = DB::table('projects')
->join('employees', 'projects.pm_id', '=', 'employees.id')
->where('projects.id', '=', $pid)
->select('projects.id', 'projects.pm_id', 'employees.name')
->first();
return view('projects/all', [
'projects' => $projects, 'am' => $am, 'pm' => $pm
]);
}
查看:
<h1 class="text-center">All Projects</h1>
@foreach ($projects as $project)
<div class="row">
<div class="col-md-6 text-center">
<h3><a href="">{{ $project->company }}</a></h3>
</div>
<div class="col-md-6 text-center">
<p>Account Manager: {{ $am->name }}</p>
<p>Project Manager: {{ $pm->name }}</p>
</div>
</div>
<hr>
@endforeach
所以我想查看每个项目的公司名称和项目经理和客户经理。不过,我看到的是同一个人负责所有项目。它没有为每个项目挑选正确的经理。
我尝试在控制器中执行 for 循环以获取项目 ID 并将其传递到 where 子句中,但这没有用。
有什么解决办法吗?
谢谢!
您对 $am 和 $pm 的查询不在 foreach 循环内。所以你只得到最后一个项目的 $am 和 $pm 值($pid)
public function all()
{
$projects = Project::all();
foreach($projects as $project) {
$pid = $project->id;
$am = DB::table('projects')
->join('employees', 'projects.am_id', '=', 'employees.id')
->where('projects.id', '=', $pid)
->select('projects.id', 'projects.am_id', 'employees.name')
->first();
$pm = DB::table('projects')
->join('employees', 'projects.pm_id', '=', 'employees.id')
->where('projects.id', '=', $pid)
->select('projects.id', 'projects.pm_id', 'employees.name')
->first();
$project->am = $am;
$project->pm = $pm;
}
return view('projects/all', [
'projects' => $projects
]);
}
查看
<h1 class="text-center">All Projects</h1>
@foreach ($projects as $project)
<div class="row">
<div class="col-md-6 text-center">
<h3><a href="">{{ $project->company }}</a></h3>
</div>
<div class="col-md-6 text-center">
<p>Account Manager: {{ $project->am->name }}</p>
<p>Project Manager: {{ $project->pm->name }}</p>
</div>
</div>
<hr>
@endforeach
我先尝试了很多不同的东西,但似乎没有得到结果。
控制器:
public function all()
{
$projects = Project::all();
foreach($projects as $project) {
$pid = $project->id;
}
$am = DB::table('projects')
->join('employees', 'projects.am_id', '=', 'employees.id')
->where('projects.id', '=', $pid)
->select('projects.id', 'projects.am_id', 'employees.name')
->first();
$pm = DB::table('projects')
->join('employees', 'projects.pm_id', '=', 'employees.id')
->where('projects.id', '=', $pid)
->select('projects.id', 'projects.pm_id', 'employees.name')
->first();
return view('projects/all', [
'projects' => $projects, 'am' => $am, 'pm' => $pm
]);
}
查看:
<h1 class="text-center">All Projects</h1>
@foreach ($projects as $project)
<div class="row">
<div class="col-md-6 text-center">
<h3><a href="">{{ $project->company }}</a></h3>
</div>
<div class="col-md-6 text-center">
<p>Account Manager: {{ $am->name }}</p>
<p>Project Manager: {{ $pm->name }}</p>
</div>
</div>
<hr>
@endforeach
所以我想查看每个项目的公司名称和项目经理和客户经理。不过,我看到的是同一个人负责所有项目。它没有为每个项目挑选正确的经理。
我尝试在控制器中执行 for 循环以获取项目 ID 并将其传递到 where 子句中,但这没有用。
有什么解决办法吗?
谢谢!
您对 $am 和 $pm 的查询不在 foreach 循环内。所以你只得到最后一个项目的 $am 和 $pm 值($pid)
public function all()
{
$projects = Project::all();
foreach($projects as $project) {
$pid = $project->id;
$am = DB::table('projects')
->join('employees', 'projects.am_id', '=', 'employees.id')
->where('projects.id', '=', $pid)
->select('projects.id', 'projects.am_id', 'employees.name')
->first();
$pm = DB::table('projects')
->join('employees', 'projects.pm_id', '=', 'employees.id')
->where('projects.id', '=', $pid)
->select('projects.id', 'projects.pm_id', 'employees.name')
->first();
$project->am = $am;
$project->pm = $pm;
}
return view('projects/all', [
'projects' => $projects
]);
}
查看
<h1 class="text-center">All Projects</h1>
@foreach ($projects as $project)
<div class="row">
<div class="col-md-6 text-center">
<h3><a href="">{{ $project->company }}</a></h3>
</div>
<div class="col-md-6 text-center">
<p>Account Manager: {{ $project->am->name }}</p>
<p>Project Manager: {{ $project->pm->name }}</p>
</div>
</div>
<hr>
@endforeach