Laravel - Multiple foreign keys and getting id

拜拜、爱过 提交于 2019-12-25 08:27:17

问题


I tried a bunch of different things first but don't seem to be getting a result.

Controller:

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
        ]);
}

View:

<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

So I want to view the company name and the project manager and account manager for each project. What I'm seeing though is the same person for all projects. It's not picking up the correct managers for each project.

I tried doing the for loop in the controller to get the project ID and passing that in the where clause but that didn't work.

Any ideas how to fix this?

Thanks!


回答1:


Your queries for $am and $pm are not inside the foreach loop. So you only gets the $am and $pm values for last project($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
        ]);
}

View

<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


来源:https://stackoverflow.com/questions/41203590/laravel-multiple-foreign-keys-and-getting-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!