How to merge two or multiple tables to each other in the Laravel PHP Framework?
How to seed Country/State data into the Database? Click Here Here, we are going to merge the Country table to the State table using left join
The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side if there is no match.

Step 1. Create a new Project in Laravel, so open git bash. Write down the following command:-
$ composer create-project --prefer-dist laravel/laravel Join "5.8.*"
Step 2. Create the authentication scaffolding and Country model. Write down the following command:-
$ php artisan make:auth
$ php artisan make:model Country -m
Step 3. Create the State model. Write down the following command:-
$ php artisan make:model State -m
Step 4. Generate a migration file into the database/migrations folder of the Country table.
public function up()
{
Schema::create('countries', function (Blueprint $table) {
$table->bigIncrements('country_id');
$table->string('country_name');
$table->string('sort');
$table->integer('phoneCode');
$table->timestamps();
});
}
Step 5. Generate a migration file into the database/migrations folder of State table.
public function up()
{
Schema::create('states', function (Blueprint $table) {
$table->bigIncrements('state_id');
$table->string('state_name');
$table->integer('country_id');
$table->timestamps();
});
}
Step 6. Migrate these tables into the MySQL database, so write the following command.
$ php artisan migrate
Step 7. Create a CountryController in App/Http/Controller folder so, write the following command.
$ php artisan make:controller CountryController --resource

Step 8. Create a StateController in App/Http/Controller folder so, write the following command.
$ php artisan make:controller StateController --resource

Step 9. Create child file resources/views/Country folder with name create.blade.php.
@extends('layouts.app') | |
@section('content') | |
<div class="container"> | |
<div class="row justify-content-center"> | |
<div class="col-md-12"> | |
<br /> | |
<h3 aling="center">Add Data</h3> | |
<br /> | |
@if(count($errors) > 0) | |
<div class="alert alert-danger"> | |
<ul> | |
@foreach($errors->all() as $error) | |
<li>{{$error}}</li> | |
@endforeach | |
</ul> | |
</div> | |
@endif | |
@if(\Session::has('success')) | |
<div class="alert alert-success"> | |
<p>{{ \Session::get('success') }}</p> | |
</div> | |
@endif | |
<form method="post" action="{{url('country')}}"> | |
{{csrf_field()}} | |
<div class="form-group"> | |
<input type="text" name="country_name" class="form-control" placeholder="Enter country Name" /> | |
</div> | |
<div class="form-group"> | |
<input type="submit" class="btn btn-primary" /> | |
<a href="{{route('country.index')}}" class="btn btn-primary" role="button">Back</a> | |
</div> | |
</form> | |
</div> | |
</div> | |
</div> | |
@endsection |
Step 10. Create an index.blade.php file within resource/views/Country/ folder.
@extends('layouts.app') | |
@section('content') | |
<div class="container"> | |
<div class="row justify-content-center"> | |
<div class="col-md-12"> | |
<h3 align="center">Country Data</h3> | |
<br /> | |
@if(session()->has('message')) | |
<div class="alert alert-success" role="alert"> | |
<strong>Success: </strong>{{session()->get('message')}} | |
</div> | |
@elseif(session()->has('error')) | |
<div class="alert alert-danger" role="alert"> | |
<strong>Error: </strong>{{session()->get('error')}} | |
</div> | |
@endif | |
Total:{{$countries->total()}} | |
Current page:{{$countries->count()}} | |
<div align="center"> | |
<a href="{{route('country.create')}}" class="btn btn-primary">Add</a> | |
<div align="right"> | |
<ul class="pagination justify-content-end" > | |
<form class="d-none d-sm-inline-block form-inline mr-auto ml-md-3 my-2 my-md-0 mw-100 | |
navbar-search" action="{{url('/search')}}" method="post" role="search"> | |
{{csrf_field()}} | |
<div class="input-group"> | |
<input type="text" name="search"class="form-control bg-light border-0 small" placeholder="Search for..." aria-label="Search" aria-describedby="basic-addon2"> | |
<div class="input-group-append"> | |
<span class="input-group-prepend"> | |
<button class="btn btn-primary" type="submit"> | |
<i class="fas fa-search fa-sm"></i> | |
</button> | |
</span> | |
</div> | |
</div> | |
</form> | |
</ul> | |
</div> | |
</div> | |
<table id ="datatable" class="table table-bordered table-striped"> | |
<thead class="thead-dark"> | |
<tr> | |
<th>Country Name</th> | |
<th>Edit</th> | |
<th>Delete</th> | |
</tr> | |
</thead> | |
@foreach($countries as $user) | |
<tr> | |
<td>{{$user->country_name}}</td> | |
<td><a href="{{action('CountryController@edit',$user->country_id)}}" class="btn btn-warning">Edit</a></td> | |
<td> | |
<form method="post" class="delete_form" action="{{action('CountryController@destroy',$user->country_id)}}"> | |
{{csrf_field()}} | |
<input type="hidden" name="_method" value="DELETE" /> | |
<button type="submit" class="btn btn-danger">Delete</button> | |
</form> | |
</td> | |
</tr> | |
@endforeach | |
</table> | |
<td colspan="3" align="center"> | |
{{ $countries->links() }} | |
</td> | |
</div> | |
</div> | |
</div> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
<script> | |
$(document).ready(function(){ | |
$('.delete_form').on('submit', function(){ | |
if(confirm("Are you sure you want to delete it?")) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
}); | |
}); | |
</script> | |
@endsection |
Step 11. Create edit.blade.php file. In this file within resources/views/Country folder.
@extends('layouts.app') | |
@section('content') | |
<div class="container"> | |
<div class="row"> | |
<div class="col-md-12"> | |
<br /> | |
<h3>Edit Record</h3> | |
<br /> | |
@if(session()->has('message')) | |
<div class="alert alert-success" role="alert"> | |
<strong>Success: </strong>{{session()->get('message')}} | |
</div> | |
@elseif(session()->has('error')) | |
<div class="alert alert-danger" role="alert"> | |
<strong>Error: </strong>{{session()->get('error')}} | |
</div> | |
@endif | |
<form method="post" action="{{action('CountryController@update', $country->country_id)}}"> | |
{{csrf_field()}} | |
<input type="hidden" name="_method" value="PATCH" /> | |
<div class="form-group"> | |
<input type="text" name="country_name" class="form-control" value="{{$country->country_name}}" placeholder="Enter Country Name" /> | |
</div> | |
<div class="form-group"> | |
<input type="submit" class="btn btn-primary" value="Edit" /> | |
<a href="{{route('country.index')}}"class="btn btn-danger">Cancel</a> | |
</div> | |
</form> | |
</div> | |
</div> | |
</div> | |
@endsection |
Thanks



With MotoShare.in, you can book a bike instantly, enjoy doorstep delivery, and ride without worries. Perfect for travelers, professionals, and adventure enthusiasts looking for a seamless mobility solution.