Official documentation: https://laravel.com/docs/7.x/eloquent-relationships
Questions tagged [eloquent-relationship]
944 questions
2
votes
2 answers
Laravel : Reaching nested relationship
Knowing the structure below, can I add a function to the Container class to get records directly with Container::with('records')->get() instead of Container::with('boxes.letter.records')->get() ?
containers hasMany boxes hasOne letter hasMany…

DevonDahon
- 7,460
- 6
- 69
- 114
2
votes
3 answers
Get model which has all specific related models
I have no idea how to solve my problem.
I have Model1 with relation:
class Model1 extends BaseModel
{
public function details(): BelongsToMany
{
return $this->belongsToMany(
Detail::class,
'model1_details',
…

Sutonne
- 68
- 6
2
votes
1 answer
How can I retrieve data from a foreignID
I am new to laravel and I am struggling to retrieve data using the query Builder.
Consider the following migration on the Hotel table:
$table->id();
$table->timestamps();
$table->foreignIdFor(User::class); …

thecodewanderer
- 29
- 5
2
votes
1 answer
Is there a way to alter the attach page to enhance specificity for a particular type of relationship?
I have a Laravel 9 project with a Nova 4 admin panel and the following set of relationships: there's the Attribute entity that defines a particular attribute (e.g. Project Type, Rating, etc.). A typical attribute is linked to several values that it…

Gevorg Melkumyan
- 628
- 1
- 12
- 24
2
votes
2 answers
Fetch specific data for Auth user from Many to Many Relationship in Laravel
How do I fetch data for specific authenticated user in a Laravel many-to-many relationship? I have a page where it will display all the latest threads from all communities. However, I want to make sure it will only show the threads from a community…

user3569641
- 892
- 1
- 17
- 50
2
votes
2 answers
Laravel eloquent with() nested relationship fields in parent unnested
the elequent query i have is:
$rows = ProjectLogin::with(
'User:PRIVATE,naam',
'Project:PRIVATE,naam',
'Queue:PRIVATE,queue_naam',
'AgentStatus:PRIVATE,status'
)
->limit(10)->get();
this will return this array if i do…

fidderr
- 53
- 8
2
votes
1 answer
Laravel 8 nested Model Relationships
This is my first ever Laravel project, please bear with us. I am trying to display a parent->child hierarchy (not recursive), something akin to below:
Category 1
----List item
----------Resource 1
----List item
----------Resource 2
Category…

Matt Allen
- 75
- 1
- 9
2
votes
3 answers
Laravel Eloquent Relationship - not fetching based on condition
I wanted to fetch all id & name from User table which satisfies the condition , order_userId of Orders table == id of User table and order & uploadId columns of Orders table has the values false & null respectively. The below code returns all rows…
user17189691
2
votes
1 answer
Where Condition on relationship in Laravel
I am trying to fetch users based on their role_id from database through ajax. I did this using below code.
$queryModel->whereHas('roles',function ($query) use ($role_id) {
$query->where("role_id",$role_id);
});
But on…

Saad Ramay
- 152
- 1
- 11
2
votes
2 answers
Check user if is in chat room Laravel
I have models:
ChatRoomMembers
ChatRoom
so I want to check if auth user is in chat room
my relationships:
ChatRoom:
public function chatRoomMembers()
{
return $this->hasMany(ChatRoomMember::class);
}
ChatRoomMembers:
…

SicklyDZN
- 31
- 3
2
votes
0 answers
how to get category->name with pivot table in eloquent
I have four models post postcomment postcategory and category I want to get all the posts with their comments and categories. postcategory table having post_id and category_id while category table has id and category_name fields in it
I am able to…

mohsin ali
- 31
- 3
2
votes
1 answer
How to get foreign key from eloquent collection in laravel
I have hasMany relationship in my User model;
/**
* Get the posts for the users.
*/
public function posts()
{
return $this->hasMany(Posts::class); //foreign key assigned by user_id
}
I need to get a foreign id in Eloquent…

ORHAN ERDAY
- 1,020
- 8
- 31
2
votes
1 answer
Laravel query where column value is greater than sum of related model column values
I have two tables transactions and transaction_allocations. One transaction can have many allocations.
Transaction Model
Schema::create('transactions', function (Blueprint $table) {
$table->bigIncrements('id');
…

Lucid Polygon
- 542
- 9
- 26
2
votes
1 answer
Nested relationship access with Laravel 8
This is the problem. I have a booking table. It's properties are as follows
-id
-path_id
-start_time
The path table is as follows
-id
-car_id
-fare
-start_location
-end_location
the car…

lebron34
- 23
- 2
2
votes
1 answer
Laravel Eloquent relationship - multiple columns of table reference same foreign key
Laravel/Eloquent newbie here. I am implementing a simple board game. Each game has 4 players. The tables structure consists of a Players table and a Games table:
SELECT * FROM players;
id | name |
---------------------
1 | John …

pazof
- 944
- 1
- 12
- 26