Lavavel Eloquent: Relationships with 5 table - laravel

I have an app consisting of these tables
users
id
name
user_location
id
user_id
cities_id
cities
id
state_id
country_id
name
states
id
country_id
name
countries
id
name
Then how to get relationship this five table?

you have to create a many-to-many relationship between tables and use pivot table to show the relationship between table rows using foreignId

Related

How to access the fields in the table in many-to-many intermediate tables?

How to access the fields in the table in many-to-many intermediate tables?
What kind of eloqunet builder is required to access the isAlternative field in the table named category_product?
Category table
categories
- id
- name
Product table
products
- id
- name
- price
CategoryProduct table
category_product
- category_id
- product_id
- isAlternative

Laravel 5.3 query to get results from 4 tables which has connections via Foreign Key

I'm using Laravel 5.3. I've 4 tables.
Default Users table. Departments, Position, Employees tables.
Users table has ID | Email | Password
Departments table has ID | Department | User_Id - Here User_Id is foreign key comes from Users table's ID
Positions table has ID | Position | Department_Id - Here Department_Id is foreign key comes from Departments table's ID
Employees table has ID | Employee | Position_Id - Here Position_Id is foreign key comes from Positions table's ID
User can have multiple Departments. Departments can have multiple Positions, Positions can have multiple Employees. So, if user is different, how can i retrieve all data from all 4 tables which that user had created?
You can use nested eager loading:
$departments = Department::where('user_id', $id)
->with('positions', 'positions.employees')
->get();
Another way is to build simple queries:
$departments = Department::where('user_id', $id)->get();
$positions = Position::whereIn('department_id', $departments->pluck('id'));
$employees = Employee::whereIn('position_id', $positions->pluck('id'));

Laravel many to many pivot table accessor

I have a Users table and a Votes table and a vote_users table (many to many relationship)
In the vote_user table I have
user_id|vote_id| anonymous.
How can I change the user_id to -1 (only when sending to client) , if anonymous column has value TRUE ?

Save on different tables using Eloquent Laravel Relationships

Hi I have 3 tables "temp_quotes", "quotes" and "quote_items"
Every quote generated goes to the "temp_quotes" table at first.
At the end of my multi-step form I need the following:
1- Receive all the quotes from the "temp_quotes" table, send it to the "quote_items" table and delete it from the "temp_quotes" table
2- Generate a new entry in the "quotes" table referencing the "users" table and
the "quote_items" table
Here is all the data and how needs to be save/removed on the tables:
temp_quotes Table
ID
BRAND
MODEL
YEAR
quotes Table:
ID
USER_ID
QUOTE_ID
quote_items Table:
ID
QUOTE_ID
BRAND
MODEL
YEAR
Any help will be appreciated.

Eloquent relationships laravel

I have 3 tables
categories
id
name
categories_product
id
category_id
product_id
and a product table
products
id
name
I want products based on catergori_id, how kan i do that with laravel Eloquent?
Simple as this:
Product::whereCategori_id($category)->get();

Resources