Official documentation: https://laravel.com/docs/7.x/eloquent-relationships
Questions tagged [eloquent-relationship]
944 questions
1
vote
2 answers
Laravel working with nested collection delete nested branch
unable to delete all nested branches where "product" => null
Have a query
$cartWhere['user_id'] = $user_id;
$cartWhere['site_id'] =$currentSite->id;
$item = Cart::select('product_id','quantity')->with(['product' => function($product) use…

Hik200
- 107
- 11
1
vote
2 answers
Laravel : Get main level category on a tree parent child relationship
I have a multi level Category model , each Category can have childs and parent on the same table (tree structure) on the same sql table.
public function childs(){
return $this->hasMany('App\Category', 'parent_id', 'id');
}
public function…

mehdi
- 119
- 1
- 11
1
vote
1 answer
Proper Way to define a hasManyThrough Relationship
i have 3 tables:
User model
|---------------------|------------------|
| column | type |
|---------------------|------------------|
| id | uuid …

joekenpat
- 387
- 3
- 16
1
vote
1 answer
Eloquent - Return Registry only if it doesn't have a value asossiated in the relationship
I have this question, maybe someone can help me :)
Student::whereHas('courses.group', function ($query) use ($year) {
$query->where('year', '!=', $year);
})->orDoesntHave('courses')->where('active', '=', '1')->get()
I have this…

Diego Alejandro Lopez
- 61
- 10
1
vote
1 answer
Laravel Many To Many Relationship with unwanted limit of 250
I've a Many To Many Relationship
users
id - integer
name - string
apps
id - integer
steam_id - integer (ID for this app on steam)
name - string
app_user
user_id - integer
app_id - integer
My user modal
public…

Norman Huth
- 519
- 5
- 16
1
vote
2 answers
Laravel - virtual column id -> uuid
I have two models:
Ingredient, Category
Each ingredient has a category id, each category has many ingredients.
The problem is that I use auto-increment ids for joins and queries internally, but only show UUIDs to end users.
So the Ingredient table…

DeltaTango
- 821
- 2
- 9
- 19
1
vote
0 answers
Laravel - How to eager load pivot relation to prevent N+1 query?
I have four tables:
Orders:
1. id
2. number
OrderProduct:
1. order_id
2. product_id
3. unit_id
4. quantity
Products:
1. id
2. name
Units:
1. id
2. name
And I use belongsToMany to define the relation between Order and Product:
class Order extends…

Thomas Yu
- 87
- 6
1
vote
1 answer
Laravel Eloquent Relationship with different foreign key
Laravel version is 7.0:
I have setup model relationships like this.

LoveCoding
- 1,121
- 2
- 12
- 33
1
vote
1 answer
Laravel Bulk Detach
First, I use detach() in laravel with this approach and this is work!
$student = \App\StudentRegistrars::withTrashed()->findOrFail($id);
$student->father_registrars()->detach();
$student->mother_registrars()->detach();
But when I am trying to…

Sead Lab
- 191
- 3
- 20
1
vote
1 answer
relational database in eloquent laravel
hay i have table schema like this
table transaction_fueler
id | code | user_id |user_name| ref_code | area_name
table user_community
user_id | community_id
table user_community is pivot table from table users and community
in table…

Bian
- 95
- 2
- 10
1
vote
1 answer
add column on a nested relationship
Is it possible to add a column on a nested relationship ?
For example, i have the following tables
groups
- id
- title
categories
- id
- title
- parent_id
- group_id
threads
- id
- title
- replies_count
- category_id
I want to do the following…

Orestis uRic
- 347
- 1
- 6
- 11
1
vote
1 answer
Single Query to get paginated data and all possible filters from the obtained results
I am writing a query to perform search for eCommerce. I need to meet 3 search criteria.
Search by category id
Search by keyword
Search by product type (featured, hot, new)
Also i need to extract all possible filters to show user from the filtered…

Muhammad Raheel
- 19,823
- 7
- 67
- 103
1
vote
2 answers
Understanding Eloquent One-to-many Relationships
I'm trying to understand Eloquent relationships but it seems I'm missing something in understanding it. I have:
A Meeting produces many Documents.
A specific Document can be produced for one Meeting.
Thus a one to many relationship. I am…

KD_Raj
- 189
- 3
- 16
1
vote
0 answers
Eager Loading Error: 'Call to a member function
I encountered an error and don't know why to add this error indicated : "Call to a member function addEagerConstraints() on int".
I have a WarehouseProduct model with relation.
class WarehouseProduct extends Model{
protected $fillable = [
…

Flower
- 21
- 4
1
vote
2 answers
Eloquent relationship not working in mail
I have an Order, Product and OrderProduct model. The Order has a belongsToMany relationship with Products as in
public function products(){
return $this->belongsToMany('App\Product')->withPivot('group');
}
So a user creates an order into…

Adam D.
- 45
- 5