I want to get a collection when many-to-many relations are multiple in Laravel.
I created a DB and model with the following structure,
I don't know how to get the list of projects that the user has from Auth :: id ().
(Finally, pass it in the form of return view ('hoge', [projects' =>$projects]) and turn it with foreach in view.)
(It has been confirmed that if there is only one intermediate table, it can be acquired normally. (Acquiring a project list of company, etc.))
users table
id | name | password |
---|
user_company table
id | user_id | company_id |
---|
companies table
id | name | url | overview |
---|
company_project table
id | company_id | project_id |
---|
projects table
id | name | overview | cost |
---|
Due to trial and error, various errors and problems have occurred, but
I couldn't think of a way to make the type relationship work,
I also think there is a better way to get all the columns without getting them every time.
Laravel 6.x
MySQL
Please do not hesitate to tell us if there is a lack of information.
-
Answer # 1
Related articles
- php - about laravel's associative array
- php - about @foreach in laravel's blade file
- [php] about the relationship between the number of select statements and the reading speed
- php - [laravel] return cannot return multiple variables
- php - laravel get information about the user who clicked
- php - about array curly braces
- i can't use foreach multiple times with php as html embedding
- i want to know how to resize and upload multiple image files with jquery + php
- php - about dockerfile
- excel - about printing (selection) of multiple sheets using openpyxl
- php - about the speed of data transfer
- php - i want to count multiple columns of the same format in sql at once
- about php date function
- cakephp/crud: about query parameter value retrieval
- php - [laravel] about the return type of the helper function view ()
- php - i want to run it on the specified date in laravel's browser test
- php - about bookmark function on the web
- javascript - about sending and receiving multiple forms using flask + websocket
- mysql - about database structure and how to hold data
- php artisan migrate could not find driver
- php : How to group fields into separate entities
- php : Retrieving data from related tables
- php : How to store an array via foreach
- php : Cookies are refreshed with a delay
- php : Variation of page output based on the existence of data in the database
- php : Why does laravel need ide helper but symfony doesn't?
- php : Please advise what to read for creating api on laravel
- php : Is it obligatory on the hosting to specify the cron run time per minute for laravel?
- php : How in laravel to update linked table one-to-one?
I was able to get it by using with ().
From now on, be careful to do a little more research before posting.