Can i use php artisan db: seed to seed related tables?

Is it possible to sow related tables using the following in Laravel 5?

php artisan db:seed 

I have two tables

 users id first name projects id name 

and pivot table

 project_user project_id user_id 

I would like to create several users, several projects, and then connect the users and their respective projects.

Visiting users and projects is not a problem, but I'm not sure how to handle the pivot table.

Is it possible?

+5
source share
1 answer

Of course you can. If you use Eloquent, you can just work with normal relationships (perhaps the easiest way). Or, if you use the SQL constructor directly, you can feed the table in the same way as usual, but you need to adhere to the rules of the foreign key.

Just try it and you will see. But make sure you import the classes you use.


Adding a relationship between the two models is easy, but there are some differences between the types of common relationships (and its perspective): one-to-many, many-to-one and many-to-many.


One-to-many and many-to-one

It is assumed that each of your projects has a creator, an owner, so to speak, you can have a 1: n ratio between User and Project .

 public class User { public function ownedProjects() { return $this->hasMany('App\Project'); } } public class Project { public function owner() { return $this->belongsTo('App\User'); } } 

In this regard, you can either attach Project to User , or tell Project who its owner is.

 // Attach a project to an user $project = Project::create([]); User::find($id)->ownedProjects()->save($project); // There is also a function saveMany() for an array of projects // Tell the project who his owner is $project = Project::create([]); $project->owner()->associate(User::find($id)); 


Many-to-many

In your case, we need a many-to-many relationship between Users and Projects . The syntax is slightly different, but the result is pretty straightforward. First, we need a connection between both models:

 public class User { public function projects() { return $this->belongsToMany('App\Project'); } } public class Project { public function users() { return $this->belongsToMany('App\User'); } } 

Then we can request the relation in the same way:

 $project = Project::create([]); User::find($id)->projects()->attach($project->id); 

You can also attach a whole bunch of projects, do the same on the other hand, separate models or synchronize them if you want to make sure that the exact quantity (and only this amount) is related:

 // Adds a relation for the user to projects with ids 1, 2, 4 and 6 User::find($id)->projects()->attach([1, 2, 4, 6]); // Adds the users with ids 19 and 173 to this project Project::find($id)->users()->attach([19, 173]); // Removes the user 19 from the projects relations Project::find($id)->users()->detach(19); // Removes all relations between this user and projects that are // not listed in the synchronization array and adds a relation // to all projects where none exists yet User::find($id)->projects()->sync([4, 7, 19, 6, 38]); 

This is the normal syntax for many-to-many relationships, but you can also attach models in the same way as for one-to-many relationships:

 // Creation of project could also be done before and saved to a variable User::find($id)->projects()->save(Project::create([])); 
+1
source

All Articles