Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kevbrn's avatar
Level 14

Return query with relationship results to view

Hey all, just getting my hands dirty with laravel and something has me stumped which should seem pretty simple but even after watching 5 Fundamentals, 5.4 From Scratch and Build a Forum with TDD and Laravel I cannot for the life of me pull off the simple task of sending the results of my query to my controller/view.

I have a users, projects and a pivot table, project_user. The table projects contains projects that would have a many-to-many relationship to users and also users have a many-to-many to projects.

In my User model I have...

public function projects()
    {

        return $this->belongsToMany(Project::class)->wherePivot('user_id', 1);

    }
}

// I hard coded the value of user user_id,1 but ultimately this value should be the current logged in user.

Project model

    public function users()
    {

        return $this->belongsToMany(User::class)->wherePivot('user_id', 1);


    }

In my ProjectsController

public function index()
    {
        $projects = Project::all()->where('status', 1);
                
        return view('projects.show', compact('project'));
        
    }

So this will successfully return all of the projects with a status of 1 but I only want to return the ones that have a relationship to the current logged in user.

No matter what I do I cannot access the projects() method from the User model. Should I be able to? How do I return the results of my query to the correct controller to pass to my view?

In tinker I get the results that I want, at least it would appear so...

$ php artisan tinker
Psy Shell v0.8.13 (PHP 7.1.9 — cli) by Justin Hilemanan
>>> $user = App\User::find(1);
=> App\User {#778
     id: "1",
     first_name: "Kevin",
     last_name: "XXXX",
     company_name: "XXXX",
     app_role_id: "1",
     project_id: "1",
     discipline_id: "1",
     project_role_id: "1",
     contact_id: "1",
     active_phases: "1",
     status: "1",
     verified: "0",
     email: "XXXX",
     cell_phone: "XXXXX",
     user_profile_pic: "nopic.png",
     created_at: "2017-10-28 16:48:24.000",
     updated_at: "2017-10-28 16:48:24.000",
   }
>>> $user->projects
=> Illuminate\Database\Eloquent\Collection {#774
     all: [
       App\Project {#119
         id: "1",
         ProjectName: "Viper",
         contact_id: "2",
         created_by: "1",
         status: "1",
         created_at: null,
         updated_at: null,
         pivot: Illuminate\Database\Eloquent\Relations\Pivot {#777
           user_id: "1",
           project_id: "1",
         },
         contact: App\User {#787
           id: "2",
           first_name: "Robert",
           last_name: "XXXX",
           company_name: "XXXX.",
           app_role_id: "1",
           project_id: "1",
           discipline_id: "1",
           project_role_id: "1",
           contact_id: "1",
           active_phases: "1",
           status: "1",
           verified: "0",
           email: "email address",
           cell_phone: "2223334455",
           user_profile_pic: "nopic.png",
           created_at: "2017-10-30 23:45:42.000",
           updated_at: "2017-10-30 23:45:42.000",
         },
       },
     ],
   }

Route

Route::get('projects', 'ProjectsController@index');

Appreciate the help!

Thanks, Kevin

0 likes
3 replies
bobbybouwmann's avatar
Level 88

You can do something like this

// ProjectsControler
public function index()
{
    $user = auth()->user(); // Get the current logged in user
    
    $user->load('projects'); // Load the relation of the projects on the user

    // Or with an extra query
    $user->load(['projects' => function ($query) {
        $query->where('status', 1);
    }); // Load the relation of the projects on the user

    $projects = $user->projects;

    return view('projects.index, compact('projects'));
}

This way you do it from the user. However you can also do it from the projects side

public function index()
{
    $projects = Project::where('user_id', auth()->id())
        ->where('status', 1)
        ->get();

    return view('projects.index, compact('projects'));  
}
kevbrn's avatar
Level 14

That did it! Oh man, thank you bobbybouwmann! Have been pulling my hair out on this one.

So let me ask; as it will work from both the user and project side, is one more correct than the other?

Also, I had something similar to

    $user = auth()->user(); // Get the current logged in user
    
    $user->load('projects'); // Load the relation of the projects on the user

But PHPstorm is telling me that method 'load' could not be found so I removed it, however it does work when I run it?

Thank you again so very much!

Kevin

bobbybouwmann's avatar

Aah yeah, the load method is a EloquentModel method, but the User model is not directly extending this class. It's like a magic method and that's why PHPStorm doesn't see it. Nothing to worry about.

Indeed you can fetch projects on both ways for a user. However you should who is the owner of the data. Is that the user or the project? Always go from there ;)

1 like

Please or to participate in this conversation.