The problem of laravel correlation Model

I just started to learn the association model of laravel, and I was so confused that sometimes I really felt that it was not as convenient as writing sql directly.

now there is a problem
role tables admin_roles and admin_permissions tables have a many-to-many relationship.

the intermediate table admin_role_permissions has only two fields
role_id and permission_id. Corresponds to the id of the admin_roles and admin_permissions tables, respectively.

now I want to check the id value of admin_permissions table when id is 1 and 2 in admin_roles table. You can write

in sql.
SELECT
    DISTINCT`admin_role_permissions`.permission_id
FROM
    `admin_permissions`
INNER JOIN `admin_role_permissions` ON `admin_permissions`.`id` = `admin_role_permissions`.`permission_id`
WHERE
    `admin_role_permissions`.`role_id` IN (1, 2)

data obtained

clipboard.png

laravel ,
AdminRoleController.php


AdminRole.php


clipboard.png

clipboard.png

the data is so complex, there are too many levels, and it hasn"t been deduplicated yet. How can we get the data results executed by the top sql? I don"t want to write the native sql
how to find the Permissions=. The value in this form is
how to rewrite it with the model, or how to get it with the collection.

Mar.24,2022

first of all, make sure that model associations are mainly used for business logic.
secondly, it is not necessary to force the use of the association model, let alone the background management.
in addition, you do not provide table structure table data, and no one else can do further testing, which is one of the reasons why your question has not been answered, and the description of the question is not clear enough.
finally, I canceled the stamping on this issue, and I also hope that more people will discuss it. As for why I stepped on it, I have already mentioned it in the comments.


gives you one more choice. Permission control can be done with PHP-Casbin
support Laravel extensions Laravel-Authz : https://github.com/php-casbin.
supports rbac, uri permissions, restful, middleware, cache, etc.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b32e43-2b5c9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b32e43-2b5c9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?