I
I
Ilya Loopashko2021-04-01 11:07:53
Database migration
Ilya Loopashko, 2021-04-01 11:07:53

How to create a relationship?

Hello. I can't figure out how to properly create relationships for two tables.

There are two tables. role and category. How to make it so that a user with the Admin role can see all categories, and with the User role only the Private category. Category IDs can change dynamically

Category:
id: 1
title: home
id: 2
title: work
id: 3
title: private

Role:
id: 1
title: Admin
id: 2
title: User

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Loopashko, 2021-04-01
@deadloop

Created an intermediate table. I will try with her.

Schema::create('categories_roles', function (Blueprint $table) {
            $table->bigInteger('category_id')->unsigned();
            $table->bigInteger('role_id')->unsigned();

            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');

            $table->primary(['category_id', 'role_id']);

            $table->timestamps();

        });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question