John Davidson

php - Migration Foreign Key Vs Eloquent Relationships in Laravel

0 comments
Message:


In Laravel 5.1 I can see that table column relationships can be set-up in 2 ways:



1) Defining Foreign Keys in the Migration table.



2) Defining the Eloquent relationships in the Models.



I have read the documentations and I am still confused on the following:




  1. Do I need to use both or only 1 is needed?


  2. Is it wrong to use both at the same time? Or does it make it
    redundant or cause conflicts?


  3. What is the benefit of using Eloquent relationships without mentioning the
    Foreign keys in migration column?


  4. What is the difference?




These are the codes I have now. Its still unclear to me if I need to remove the foreign keys I have set-up in my migration file.



Migration:



  public function up()
{

Schema::create('apps', function (Blueprint $table) {
$table->increments('id');
$table->string('app_name');
$table->string('app_alias');
$table->timestamps();
$table->engine = 'InnoDB';
});

// This is the second Migration table
Schema::create('app_roles', function (Blueprint $table) {
$table->increments('id');
$table->integer('app_id')->unsigned()->index();
$table->integer('user_id')->unsigned()->index();
$table->integer('role_id')->unsigned()->index();
$table->engine = 'InnoDB';

$table->unique(array('app_id', 'user_id'));

$table->foreign('app_id')
->references('id')
->on('apps')
->onDelete('cascade');

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

$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade');
});
}


Model with Eloquent Relationships:



// App Model
class App extends Model
{

public function appRoles() {
return $this->hasMany('App\Models\AppRole');
}
}

// AppRole Model
class AppRole extends Model
{
public function app() {
return $this->belongsTo('App\Models\App');
}

public function user() {
return $this->belongsTo('App\User');
}

public function role() {
return $this->belongsTo('App\Models\Role');
}
}

// User Model
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
.....
public function appRole() {
return $this->belongsToMany('App\Models\AppRole');
}
}

// Role Model
class Role extends EntrustRole
{
public function appRole() {
return $this->hasMany('App\Models\AppRole');
}
}


Can someone help me understand this please?



My Answer:
> Foreign keys in the migration table are used to define the relationship between two tables at the database level, while Eloquent relationships in the models are used to define the relationship at the application level.

> You can use both foreign keys in the migration table and Eloquent relationships in the models, and it is not wrong to do so. Using both can provide clarity and consistency in your application.

> The benefit of using Eloquent relationships without mentioning foreign keys in the migration column is that it allows you to define the relationship in a more abstract and object-oriented way. Eloquent relationships provide a more expressive and intuitive way to work with relationships in Laravel.

> The difference between using foreign keys in the migration table and Eloquent relationships in the models is that foreign keys define the relationship at the database level, while Eloquent relationships define the relationship at the application level. Using both can provide a comprehensive and well-structured approach to managing relationships in your Laravel application.

Rate this post

4 of 5 based on 3725 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us