Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel relationships in migrations?

I know you can define table relationships fairly easy with $this->belongs_to(), $this->has_many() etc, but what i don't understand is how the relationship table is created; the table that binds the two tables together (i forgot what the term is called).

Let's say i'm creating a users table. I want that user to belong to a certain "Role". There are multiple roles, and every role can have multiple users. I will need to also create a roles table for that. So far, so good.

But after reading the documentation, it says i should add the $this->belongs_to() in the model, not the migration itself. When, and how is the relationship table created? If i create the roles and users tables, and add $this->belongs_to('roles') to the users model, and $this->has_many('users') to the roles model, will the middle table be created automatically?

like image 758
qwerty Avatar asked Nov 24 '12 12:11

qwerty


People also ask

How many relationships are there in Laravel?

As you know, in relationships between tables in the database, we usually have 3 types of relationships. These relationships are called one-to-one, one-to-many, and many-to-many relationships.

How do you add a relationship in Laravel?

To define a relationship, we need first to define the post() method in User model. In the post() method, we need to implement the hasOne() method that returns the result. Let's understand the one to one relationship through an example. First, we add the new column (user_id) in an existing table named as posts.

What is difference between migrate fresh and refresh in Laravel?

php artisan migrate:fresh is used when we want a fresh or new installation of our database. It deletes all the existing tables of the database and runs the migrate command. php artisan migrate:refresh is a two in one command that executes the :rollback command and the migrate command.


2 Answers

When creating a migration you can specify foreign keys on your tables, i.e.

public function up() {     Schema::table('roles', function(Blueprint $table) {         $table->increments('id');         $table->integer('user_id')->unsigned();         //rest of fields then...         $table->foreign('user_id')->references('id')->on('users');     }); } 

This will create a foreign key on the user_id column on the roles table. The benefits of foreign keys is that when an update or delete is made the foreign key table will be automatically updated or "cascaded" great description found here

As described on the Laravel documentation you could also specify your cascading on update using the following syntax

$table->foreign('user_id')   ->references('id')->on('users')   ->onDelete('cascade'); 

I would do a bad job of trying to explain it better than the documentation does so please have a read through the "Relationships" section of the Eloquent ORM documentation to see how its done.

like image 122
Roark Avatar answered Oct 07 '22 00:10

Roark


It looks like a few of the initial questions were never answered, i.e. "When, and how is the relationship table created" & "will the middle table be created automatically":

As far as I am aware, these tables need to be created manually. So create the migration file like so:

Laravel 5

php artisan make:migration create_role_user_table 

Laravel 4

php artisan migrate:make create_role_user_table 

Note that the names are singular, and are presented in alphabetical order.

Then in the migration something like:

public function up() {     Schema::create('role_user', function($table) {         $table->increments('id');         $table->integer('role_id');         $table->integer('user_id');         $table->timestamps();     }); } 

Hope that helps. I'm not sure if the timestamps are needed in Pivot Tables or not, so please experiment.

like image 30
Mere Development Avatar answered Oct 06 '22 23:10

Mere Development