Search code examples
laravellaravel-5model-associationslaravel-blade

Laravel 5, Filter Data Base Query using Association / Pivot table


What I want is a listing of all the Industries associated with a Project. What I can get is all the Industry IDs per Project using an association table. I can not figure out how to return each Industry->Name from those IDs.

I have a three tables in my Data Base: projects, projects_industries, and industries. The 'projects_industries' table has 'id', 'project_id', and 'industries_id'.

The following code returns a blank html page. Thanks for your help / suggestions!

ProjectsController :

public function show(Project $project){
....$projectsindustries = DB::table('projects_industries')->select('*')->where('projects_id', $project->id)->get();
....$industries = Industry::all();
....return view('projects.show', compact('project', 'projectsindustries', 'industries'));
}

BTW, I know the $projectsindustries DB Query works

Blade View :

@if($projectsindustries)
....<ul>
........@foreach($projectsindustries as $projectindustry)
............@foreach($industries as $industry)
................<li><a href="#">{{ $industry::where('id', '=', '$projectindustry->industries_id')->get()->name; }}</a></li> 
............@endforeach
........@endforeach
....</ul>
@else
....<p>no industries.</p>
@endif

Solution

  • Here is how I eventually fixed my issue...

    The DB Tables and Fields remain the same

    Projects Controller

    <?php namespace App\Http\Controllers;
    
    use DB;
    use App\Project;
    use App\ProjectIndustry;
    use App\Industry;
        public function show(Project $project)
        {
        $projectsindustries = ProjectIndustry::where('projects_id', '=', $project->id)
            ->join('industries', 'industries_id', '=', 'industries.id')->get();
    
        return view('projects.show', compact('project', 'projectsindustries'));
        }
    

    Project Model

    use Illuminate\Database\Eloquent\Model;

    class Project extends Model {

    public function industries() {
        return $this->belongsToMany('Industry', 'projects_industries', 'projects_id', 'industries_id');
    }
    

    Project Industry Model

    use Illuminate\Database\Eloquent\Model;

    class ProjectIndustry extends Model {

    protected $table = 'projects_industries';
    
    public function project()
    {
    return $this->belongsTo('Project', 'id', 'projects_id');
    }
    
    public function industry()
    {
    return $this->hasMany('Industry', 'id', 'industries_id');
    }
    

    Industry Model

    <?php namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Industry extends Model {
    
        public function project()
        {
        return $this->belongsTo('Project');
        }
    
    }
    

    Project View projects/show.blade.php

    @if($projectsindustries)
    <h3>Industries:</h3>
        <ul>
            @foreach($projectsindustries as $projectindustry)
                <li><a href="{{ route('industries.show', $projectindustry->slug) }}">{{ $projectindustry->name }}</a></li>
            @endforeach
        </ul>
    @else
        <p>no industries.</p>
    @endif
    

    I hope this can be of help to someone.