How you design your database schema, that defines your knowledge about databases. Sometimes we need to design a tree database schema like a product has many categories or a category has many sub-categories or even more, a sub-category has many sub sub categories. You can say that this is a laravel hierarchical relationship. Right, this scenario is exactly laravel tree relationship.
Now in this tutorial, we will see how we can implement laravel parent child same table relationship. Assume there is a Department model and this department may have many sections and the section may have many teams like below:
- Department
- Section 1
- Team 1
- Team 2
- Team 3
- Section 2
- Team 1
- Team 2
- Team 3
.
.
.
Now we will create a model Department and we will define laravel relationship with same table. So from this tutorial, we will learn a complete real-life example of laravel self relationship.
So before creating this laravel eloquent parent child same table relationship, create a department model by the below command:
php artisan make:model Department -m
Now update the migration file like this:
<?php
use App\Models\Department;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('departments', function (Blueprint $table) {
$table->id();
$table->string('department_name');
$table->foreignIdFor(Department::class)->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('departments');
}
};
Now run php artisan migrate and check the database tables like this:
Now update the department model like:
app/Models/Department.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Department extends Model
{
use HasFactory;
public function parent()
{
return $this->belongsTo(static::class,'department_id');
}
public function children()
{
return $this->hasMany(static::class, 'department_id')
->orderBy('name', 'asc');
}
public function allChildren()
{
return $this->children()->with('allChildren');
}
}
Now you can fetch a child or all children from the controller like this:
app/Http/Controllers/TutorialController.php
<?php
namespace App\Http\Controllers;
use App\Models\Department;
class TutorialController extends Controller
{
/**
* Tutorial from LARAVELIA
*/
public function index()
{
$department = Department::find(3);
$parent = $department->parent;
$department = Department::find(3);
$children = $department->children;
$department = Department::find(3);
$children = $department->allChildren;
}
}
Read also: How To Limit Eloquent Relationship Result In Laravel?
Now you can also write query like this:
app/Http/Controllers/TutorialController.php
<?php
namespace App\Http\Controllers;
use App\Models\Department;
class TutorialController extends Controller
{
/**
* Tutorial from LARAVELIA
*/
public function index($id)
{
$department = Department::where('id',$id)->with([
'children'=> function($query){
$query->with([
'children'=> function($query){
$query->with(['children'=> function($query){
$query->with('children');
}]);
}]);
}])->select(['id'])->get();
}
}
All are set to go. Now you can get your child, parent and all child very easily.
Read also: Prevent Lazy Loading To Force Eager Load In Laravel
Conclusion
In this laravel parent child same table tutorial, I have tried my best to let you know laravel relationship with same table. Hope now after completing this laravel hierarchical relationship tutorial, you will know this laravel tree relationship and also know how to implement get parent and child category laravel.