Laravel's Eloquent hasManyThrough - laravel

Is there possible to do a hasManyThrough getting user's country id from a Many to Many pivot table (country_user) ? something like
countries
id - integer
name - string
users
id - integer
name - string
posts
id - integer
user_id - integer
title - string
country_user
user_id - integer
country_id - integer

Related

Laravel 5.2 - Can You Create A Relationship Between A Pivot Table And A Table It's Not Pivoting For?

I have a pivot table called user_store. It's used to establish a many to many relationship between the users table and the stores table and looks something like this:
user_id - int
store_id - int
user_age_range - string
I have another table called user_memberships. It's columns look like:
user_id - int
store_id - int
membership_cost - decimal
membership_expiration - date
I need to establish a relationship between the user_memberships table and the user_store pivot table using the user_id and store_id columns present in both tables.
How would I do this when the user_store table does not have its own model (since it's a pivot table)?

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

Lavavel Eloquent: Relationships with 5 table

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

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'));

How to create a one-to-many relationship in Eloquent to a table without creating a model for that table?

I have a model which holds a property with zero to many values. Those values are strings (e-mail addresses). But I don't want to create an extra model for such values since they only appear in this property.
As far as I read the docs I need to have a model for my e-mail addresses to gain full power of Eloquent.
Am I missing something out or is there no clean way to spare a model for a database table for relationships?
A short example of my database tables in question:
Table A:
- id [serial]
- name [string]
- someProperty [string]
- mailAddresses [unsigned int; reference to id of Table B]
Table B:
- id [serial]
- mail [string]
I've got a model for Table A:
class ModelA extends Eloquent {
protected $table = 'Table A';
public function mailAddresses() {
return $this->hasMany('<what to put here?>');
}
}
Bascially, you have 1 Model for 1 Database Table (except the Table is an intermediate table (pivot table)).
So shortly, yes. If you want Eloquent Models to work properly, you need to create a Model for your E-Mail Table.

Resources