In this post, you will learn how to do Join query in the same table or parent-child relation make using join query in laravel here below simple example given.
This case comes when I trying to make a single database table for the main category or parent and sub-category or child relation want to store in a single table.
Laravel Eloquent Parent Child In Same Table With Example
<?php
namespace App\Http\Controllers;
use App\Models\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
// same table left join just change name
$categories = Category::select(
'categories.*',
'categories.name',
'sub_category.name as sub_category_name'
)
->leftjoin(
'categories as sub_category',
'sub_category.category_id','=','categories.id'
)
->get();
return view('admin.category.category',compact('categories'));
}
}
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Livewire Create Multiple Select2 Dynamic Dropdown In Laravel 9 8 Example
- PayPal Payment Gateway Integration in Laravel 12 (2026) Step-by-Step Guide
- Laravel 12 Authentication with Breeze & Sanctum (Complete Guide 2026)
- resize image and upload example laravel intervention
- What are accessors and mutators In Laravel
- Everything know about Laravel Localization With Demo Example
- How To Use Where Clause With Two Fields Of The Same Table Examples
- Laravel 10 Arr::pluck() Helper Function Use Example
- How To Secure Laravel Website From Hackers
- How To Change ENUM DataType Without Losing Data Laravel Migration