Laravel - Multiple foreign keys and getting id

2019-09-16 13:12发布

问题:

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