dimanche 21 juillet 2019

How Assign To User In Many To Many Relationship at laravel?

I have 3 table
User Table :

| id | name | email          | password |
|----|------|----------------|----------|
| 1  | jack | jack@gmail.com | 123456   |

Sites Table :

| id | name   | url        |
|----|--------|------------|
| 1  | google | google.com |

I want to do this work On Laravel:
when a user Adds A site , Atomatically adds that site to that user on site_user table
For example when i add site google with url google.com , adds a row on site_user table with this contents :
site_user Table :

| id | user_id | site_id |
|----|---------|---------|
| 1  | 1       | 1       |

that is says site_id 1 belongs to user_id 1 . how can i do this?

I have two model site and user :
On Site Model:

public function users(){
        return $this->belongsToMany(User::class,'site_user','site_id','user_id'); 
    }

On User Model:

public function sites(){
        return $this->belongsToMany(Site::class,'site_user','user_id','site_id');  
    }

And in Controller i don't know how can i do this work! please help me@



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire