Laravel - Many to Many relationship

Nas Atchia

I have 2 tables, users and pictures. The tables have a many-to-many relationship and I have created a pivot table name user_pictures.

The users table is as follows:

id
firstname
lastname

The pictures table is as follows:

id
filename
url 

The user_pictures table contains:

id
user_id
picture_id

When a user adds a picture, the picture filename and url is stored in the pictures table, and in the user_pictures table the picture_id of that picture is stored alongside the user_id of that user.

How can I retrieve all the picture filename and url related to the user that have added them using Eloquent ORM?

ka_lin
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = 'users';
    // Define pictures with a pivot table link
    public function pictures() {
        return $this->belongsToMany('App\Models\UserPicture', 'user_pictures', 'picture_id', 'user_id');
    }
    //add methods
}

class UserPicture extends Model
{
    protected $table = 'user_pictures';
    public function picture() {
        return $this->hasOne('App\Models\Picture', 'id', 'picture_id');
    }

    public function user() {
        return $this->hasOne('App\Models\User', 'id', 'user_id');
    }
    //add methods
}

class Pictures extends Model
{
    protected $table = 'pictures';
    //add methods
}

If you don't want to add timestamps to the tables you must add these 2 methods:

 public function setCreatedAt($value) {
        // to disable created_at
    }

    public function setUpdatedAt($value) {
        // to disable created_at
    }

To fetch pictures from a user do this:

$userPictures = UserPicture::where(array('user_id'=>1)->get();
foreach($userPictures as $userpicture) {
    $picture = $userpicture->picture();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

Laravel 6 many to many relationship

From Dev

Pagination with many to many relationship in laravel

From Dev

Laravel many to many relationship error

From Dev

Laravel many to many relationship with conditions

From Dev

Laravel ORM many to many relationship

From Dev

Laravel: one to many relationship

From Dev

one to many relationship in laravel

From Dev

Laravel Polymorphic many to many relationship issue

From Dev

Laravel 5.2 Friendship system: Many to Many relationship

From Dev

Laravel many-to-many relationship on the same model

From Dev

How to get many to many relationship items in laravel

From Java

Laravel save / update many to many relationship

From Dev

Is this many-to-many relationship possible in Laravel?

From Dev

Eager loading with many to many relationship in laravel 4

From Dev

Laravel Complicated inner join on many to many relationship

From Dev

Friendship system with Laravel : Many to Many relationship

From Dev

laravel syncing many-to-many relationship

From Dev

Laravel many to many relationship using Eloquent

From Dev

how to define many to many relationship in laravel models?

From Dev

Advanced search with many to many relationship in laravel

From Dev

Laravel Many to Many query relationship with where clause

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

Laravel Many to many relationship get specific data

From Dev

Laravel Many to Many Relationship - Linking 3 items

From Dev

Laravel Removing Pivot data in many to many relationship

From Dev

Is this many-to-many relationship possible in Laravel?

From Dev

Many to Many relationship query. Laravel

Related Related

  1. 1

    Many to many relationship in Laravel

  2. 2

    Many to Many relationship with Laravel

  3. 3

    Laravel 6 many to many relationship

  4. 4

    Pagination with many to many relationship in laravel

  5. 5

    Laravel many to many relationship error

  6. 6

    Laravel many to many relationship with conditions

  7. 7

    Laravel ORM many to many relationship

  8. 8

    Laravel: one to many relationship

  9. 9

    one to many relationship in laravel

  10. 10

    Laravel Polymorphic many to many relationship issue

  11. 11

    Laravel 5.2 Friendship system: Many to Many relationship

  12. 12

    Laravel many-to-many relationship on the same model

  13. 13

    How to get many to many relationship items in laravel

  14. 14

    Laravel save / update many to many relationship

  15. 15

    Is this many-to-many relationship possible in Laravel?

  16. 16

    Eager loading with many to many relationship in laravel 4

  17. 17

    Laravel Complicated inner join on many to many relationship

  18. 18

    Friendship system with Laravel : Many to Many relationship

  19. 19

    laravel syncing many-to-many relationship

  20. 20

    Laravel many to many relationship using Eloquent

  21. 21

    how to define many to many relationship in laravel models?

  22. 22

    Advanced search with many to many relationship in laravel

  23. 23

    Laravel Many to Many query relationship with where clause

  24. 24

    Laravel 5 Eloquent count many to many relationship

  25. 25

    Laravel Many to many relationship get specific data

  26. 26

    Laravel Many to Many Relationship - Linking 3 items

  27. 27

    Laravel Removing Pivot data in many to many relationship

  28. 28

    Is this many-to-many relationship possible in Laravel?

  29. 29

    Many to Many relationship query. Laravel

HotTag

Archive