How to get the project which belongs specific user

  laravel

Database

i have 3 tables users, posts and post_user

users table     projects table      project_user table
---------       ---------           --------------------
id | name       id | name           project_id | user_id
---------       ---------           --------------------
1  | user1      1  | pr1            1      |    1
2  | user2      2  | pr2            2      |    1
---------       3  | Pr3            3      |    2
                ---------          --------------------

Models

User Model


public function projects(){
    return $this->belongsToMany(Project::class);
}

Project Model


public function users(){
    return $this->belongsToMany(User::class);
}

The project is written by laravel 8.x
Now in controller and view how to get all the projects which belongs specific user id.
all i need is to display the projects of the logged in user using laravel.

Source: Laravel

Leave a Reply