Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dpakagarwal's avatar

relationship issues in laravel 8

I have three tables namely category, subcategory, and services.

Basically, I wanna relations on this bellowed table respectively,
1) category -> has many sub-category
2) each subcategory -> has many services

Category and sub-category table

public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->text('category_name')->nullable();
            $table->integer('parents_id');
            $table->timestamps();
        });
    }

services table

public function up()
    {
        Schema::create('services', function (Blueprint $table) {
            $table->id();
            $table->text('ser_name')->nullable();
            $table->text('title')->nullable();
            $table->text('image')->nullable();
            $table->text('description')->nullable();
            $table->integer('cat')->nullable();
            $table->integer('sub_cat')->nullable();
            $table->integer('price')->nullable();
            $table->boolean('status')->nullable();
            $table->timestamps();
        });
    }

Note: on update and delete, only particular services would be get updated and deleted and on either parent category or subcategory deletion, it gives error for services available under a particular subcategory to delete first

I want this type of 'mega menu' structure for these relations:

Image

0 likes
3 replies
tykus's avatar

No idea what your question is @deepakagarwal - maybe consider giving us the error message??? Also explain what you are trying to achieve.

dpakagarwal's avatar

@tykus I want all services related to its subcategory and subcategory is dependent on parent category as parents_id displayed in the category model. but I don't get any idea how to get all services of a particular subcategory using eloquent relations

Please or to participate in this conversation.