Hi Artisan
In this tutorial i will discuss about laravel eloquent relationships example from scratch. There are many relationship in Laravel. But in this tutorial i will discuss about laravel hasmany through that mean one to many eloquent relationship example.
If you don't know how to use laravel one to many relationship laravel 8 then this tutorial is for you. So let's start hasmany laravel 8 tutorial. In this tutorial we are going to discuss about hasMany eloquent relationship example. For example, a company may have many customer, or an order could be related to the user who placed it.
Eloquent makes managing and working with these relationships easy, and supports several different types of relationships: some of them are Eloquent relationships are defined as methods on your Eloquent model classes. In this tutorial we are going to learn about Laravel hasmany eloquent relationship.
A HasMany() relationship is a very basic relation. For example a company model might be associated with many customer. and a customer model might be associated with many company. Hope you will understand its relationship.
Let’s start code to better unserstand laravel hasmany eloquent relationship.So first create a fresh laravel app to do it.
Create-Project
Run this command to download a fresh laravel app
composer create-project --prefer-dist laravel/laravel eloquent
Create Database Migration
We need two table for hasmany eloquent relationship example. One customer and other is compnay . so run two below commands to create migration
php artisan make:model Company -m
php artisan make:model Customer -m
here -m for creating migration to corresponding model . Hope you understand. after running those command we have two table in our database/migrations folder. So open this two table and paste below code to two table.
Company table
public function up()
{
Schema::create('companies', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
}
And this is our customer table
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email');
$table->integer('company_id');
$table->timestamps();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
});
}
Our table is completed. Run below command to migrate those table into our database.
php artisan migrate
Now time to setup our relationship between company and customer. Se mind that one company might have many customer. So go to your company model and paste the below code.
namespace App;
use Illuminate\Database\Eloquent\Model;
class Company extends Model
{
public function customers(){
return $this->hasMany(Customer::class);
}
}
And we will create another relationship to customer to company. So paste below code to your customer model .
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
public function company()
{
return $this->belongsTo(Company::class);
}
}
Create Controller
Now we have to create our controller. To create controller , run following command
php artisan make:controller EloquentController
After creating controller. Now paste this following code to your EloquentController.php
app/Http/Controllers/EloquentController.php
namespace App\Http\Controllers;
use App\Company;
use App\Customer;
use Illuminate\Http\Request;
class EloquentController extends Controller
{
public function Home()
{
$companies = Company::all();
return view('welcome',compact('companies'));
}
}
Now time to setup our route. to create route just paste the following code.
Route::get('/','EloquentController@Home');
Setup Blade file
Now for showing our relational data from database, Just paste the following code to welcome.blade.php.
Read also : Laravel 6 | Polymorphic Relationship Example Tutorial
Hope it can help you.
#laravel #eloquent-relationship #laravel-relationship #hasmany