Search

Laravel Eloquent Relationships (ORM) Tutorial With Example

  • Share this:
post-title

Laravel includes Eloquent, an object-relational mapper (ORM) that makes it enjoyable to interact with your database. When using Eloquent, each database table has a corresponding "Model" that is used to interact with that table.

What is laravel relationship?

Defining Relationships. Eloquent relationships are defined as methods on your Eloquent model classes. Since relationships also serve as powerful query builders, defining relationships as methods provides powerful method chaining and querying capabilities.

Laravel provides the following relationships –

  1. One To One
  2. One To Many
  3. Many To Many

Step 1: Install laravel project 

before installing make sure you have installed composer

composer create-project — prefer-dist laravel/laravel laravel-project

Step 2: Configure Database Connection

In this type of project, we must give precedence to the database connection, generically it should be configured before getting started. Incorporate the following code in .env file with your database details.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

Step 3: Create Countries Model

Create Model Controller and Migration with this magic code

php artisan make:model Countries -mc

After create migration find the migration file in database/migrations/2023_04_08_022409_create_countries_table.php

Open the file and we can add the field in the countries table see this code

public function up(){
	Schema::create('countries', function (Blueprint $table) {
   		$table->id();
   		$table->string('name');
   		$table->timestamps();
	});
}

Step 4: Create State Model

Create Model Controller and Migration with this magic code

php artisan make:model States -mc

After create migration find the migration file in database/migrations/2023_04_08_022409_create_states_table.php

Open the file and we can add the field in the states table see this code

public function up(){
	Schema::create('states', function (Blueprint $table) {
   		$table->id();
   		$table->string('name');
   		$table->foreign('country_id')->on('countries')->references('id')
   		->onDelete('CASCADE')
   		->onUpdate('CASCADE');
   		$table->timestamps();
	});
}

Step 5: Create Cities Model

Create Model Controller and Migration with this magic code

php artisan make:model Cities -mc

After create migration find the migration file in database/migrations/2023_04_08_022409_create_cities_table.php

Open the file and we can add the field in the cities table see this code

public function up(){
	Schema::create('cities', function (Blueprint $table) {
   		$table->id();
   		$table->string('name');
   		$table->foreign('country_id')->on('countries')->references('id')
   		->onDelete('CASCADE')
   		->onUpdate('CASCADE');
   		$table->foreign('state_id')->on('states')->references('id')
   		->onDelete('CASCADE')
   		->onUpdate('CASCADE');
   		$table->timestamps();
	});
}

We have describe with details and brief example one by one (ORM) relation so you can read the orm query builder laravel

One To One

One To Many 

Many To Many

Many To One

 

About author
Here’s My little description of your attention on Me and My blog. I am here to help you with PHP programming.
View all posts (53)