Official documentation: https://laravel.com/docs/7.x/eloquent-relationships
Questions tagged [eloquent-relationship]
944 questions
216
votes
18 answers
Automatically deleting related rows in Laravel (Eloquent ORM)
When I delete a row using this syntax:
$user->delete();
Is there a way to attach a callback of sorts, so that it would e.g. do this automatically:
$this->photo()->delete();
Preferably inside the model-class.

Martti Laine
- 12,655
- 22
- 68
- 102
190
votes
5 answers
Laravel orderBy on a relationship
I am looping over all comments posted by the Author of a particular post.
foreach($post->user->comments as $comment)
{
echo "" . $comment->title . " (" . $comment->post->id . ") ";
}
This gives me
I love this post (3)
This is a comment…

PrestonDocks
- 4,851
- 9
- 47
- 82
23
votes
3 answers
Sorting Laravel Collection via Array of ID's
Is it possible to order a relationship collection using a separate array of ID's while still accessing through the relationship?
The setup is Checklist has many ChecklistItems and the desired order of the related items exists as a property…

Matthew Brown
- 4,876
- 3
- 18
- 21
15
votes
1 answer
Laravel 7.x: Difference between fresh and refresh method?
While I was reading Laravel documentation, I faced a method named fresh and refresh on the Eloquent model. Please explain the major difference between them? I am having hard time understanding those.

Gursewak Singh
- 642
- 1
- 7
- 20
9
votes
2 answers
Laravels syncWithoutDetaching and additional data
I have Googled my fingers sore, and I can't see anyone discussing this, but I have a suspicion that Laravels syncWithoutDetaching() method doesn't take any parameters for extra data like save(), sync() and attach() does?
Does anybody know this? In…

rebellion
- 6,628
- 11
- 48
- 79
7
votes
2 answers
laravel eloquent - Use without on nested eager loaded relations
I'm currently working on laravel framework and I'm stuck with some relations and eager loading issues.
Situation
I have three models A, B and C
I have two relations
A has many B
B has many C
By default (using the $with attribute in Model) :
A…

Balthazar Frolin
- 73
- 1
- 5
6
votes
1 answer
Query Laravel Eloquent many to many where all id's are equal
I making a project based on Laravel and have the tables: companies, attributes, and attribute_company related as Many To Many relation when attribute_company use as a pivot table to connect companies and attributes tables.
I get an array of…

benjah
- 613
- 7
- 29
5
votes
1 answer
SQLSTATE[HY000] [2002] Connection refused error in laravel when using whereHas
Facing a strange issue in laravel. Everything works fine normally database working fine for normal read/write operations and for regular eloquent queries. But when I use
$query->whereHas('some_related_model',function($q){
…

wali razzaq
- 83
- 1
- 7
5
votes
4 answers
Delete all relation when Deleting a row in laravel
I have Posts, Comments and notifications Table
Every Post hasMany comments
every comment hasMany Notifications
every Post hasMany Notifications
class Post extends Model
{
public function notifications() {
return…

Mahmoud Ben Jabir
- 173
- 2
- 9
5
votes
1 answer
Laravel relationship Column 'id' in where clause is ambiguous
I have courses and subscription types.
I want to get all the courses that has a given subscription type.
My attempt:
$courses=Course::wherehas('subscriptionType',function ($q)
{
return $q->where('id','1');
})->get();
But…

user3844579
- 485
- 1
- 8
- 29
4
votes
1 answer
Eloquent ORM: Factory not reusing the already created model for a many-to-many relation
I'm modeling and seeding the following models using Laravel 9 and Eloquent:
Organization (1-n)
organizations_users (pivot)
User (1-n)
To seed that model, I followed the documentation and used has() and recycle()…

maxime
- 1,993
- 3
- 28
- 57
4
votes
0 answers
Eloquent eager loading relationship
Hello wonderful people of StackOverflow, I hope you all have a good day ( ˘⌣˘)♡(˘⌣˘ )
I'm new to Laravel and currently learning about eloquent relationships (hasMany)
I'm sorry this post was too long to read, but I wanted to clarify every step I…

Gwein
- 109
- 3
- 14
4
votes
3 answers
Laravel Eloquent Many to Many Relationship between three Models
Consider I have the following table structures for tables countries, products and suppliers:
countries
------------------------
id
name
code
product
------------------------
id
name
price
suppliers
------------------------
id
name
A product can…

Yogesh
- 41
- 3
4
votes
2 answers
Laravel: How to get data from 3 tables with relationship
I have 3 Tables:
Customers
id
name
Sales
customer_id
sale_date
Contacts
customer_id
contact_date
There aren't any update operations in the contacts table. Each process opens a new record in the contacts table. So, a user can have more than one…

Madame Green Pea
- 187
- 1
- 3
- 13
3
votes
2 answers
LatestOfMany() of BelongsToMany() relationship
I've been using latestOfmany() for my hasMany() relation to define them as hasOne() for quite a while now. Lately I've been in need of the similar application but for belongsToMany() relationships. Laravel doesn't have this feature unfortunately.
My…

Skywarth
- 623
- 2
- 9
- 26