Official documentation: https://laravel.com/docs/7.x/eloquent-relationships
Questions tagged [eloquent-relationship]
944 questions
-1
votes
1 answer
Laravel Eloquent: Join two models using just one SQL query (using JOIN)
I have two models. There are just two different tables, and the only common is the order number.
class Order extends Model
{
protected $primaryKey = 'OrderId';
protected $keyType = 'string';
protected $table = 'Order';
public…

kgtu
- 1
- 1
-1
votes
1 answer
Make an Eloquent immutable model magically record edits in another model/table and retrieve them
Is it possible to have a models such as Cars and Overrides where I retain the original record in Cars, but substitute any update in Overrides (signified by col/val in overrides table).
Ideally this would be global anytime the record is retrieved.
So…

john_ch
- 103
- 9
-1
votes
1 answer
Laravel many to many 2 level down pivot table
I am trying to build a Survey Module in Laravel where user can create survey, assign questions and options to the questions in survey.
survey can have multiple questions
survey question can have multiple options
To meet the above requirement I…

Ahmar Arshad
- 477
- 1
- 10
- 22
-1
votes
1 answer
How to get subcategory status and category status on Blade view?
$categories = Category::with('subcategory')->where('status',1)->take(7)->get();
view()->share('categories', $categories);
model Category:
protected $fillable =[
'category_en',
'category_np',
'status',
];
public function posts()
{
…

Online Tech
- 1
- 1
-1
votes
1 answer
Fetching Multi level related records laravel
I am using Laravel 8.
I have 3 tables users, campaigns and payments
User has many Campaigns and Campaign has many payments
table Payments has campaign_id and table campaigns has user_id
I want to fetch auth user payment details but table payments…

Rizwan Patel
- 5
- 4
-1
votes
2 answers
How would you model this in a relational database?
I have three tables: Books, Sagas and Universes.
Books has idSaga as foreign key.
Sagas has idUniverse as foreign key.
All of them are one to Many relationships.
A book can be an installment of a saga and that saga part of a bigger universe; for…

Danstern
- 127
- 8
-1
votes
2 answers
Group by and Sum of One-To-Many relation tables in Eloquent
I have a requirement.
My DB has tables like the following.
The tables have OneToMany (1-n) parent-child relation.
Table School (id, school_name)
Table Class (id, school_id, class_name)
Table Section (id, class_id, section_name, no_of_seats)
Table…

B.M.
- 318
- 2
- 3
- 19
-1
votes
1 answer
How to access old value on intermediate table while updateExistingPivot() in laravel 8
Let's say Here . I have an extra column on intermediate table 'noOfUpdates' then how can i access its previous value.
I tried this but it didn't work.
$user = User::find(1);
$user->roles()->updateExistingPivot($roleId, [
'noOfUpdates' =>…

Stark
- 3
- 2
-1
votes
1 answer
Relationship with attribute laravel 8
I want make relationship with attribute but i dont understand how to do that with laravel ORM Eloquent, can somebody help me? Thanks in advance

Ihda Anwari
- 25
- 1
- 7
-1
votes
1 answer
Query Laravel Relationships
Just as title say, i want to query query relationships(plural).
I saw numerous examples such as this
which work well. But it's querying only one relationship.
Can it just:
Vehicle::with("deliveries", "user", "type", "workOrganization")->get();
go…

Veljko Stefanovic
- 481
- 6
- 25
-1
votes
1 answer
How to Sum Price of Array of Products with their quantities in Laravel
I am trying to calculate the sum of the price of multiple products with their quantities.
The request is:
[{"product_id": 14, "quantity": 1}, {"product_id": 18, "quantity": 1}, {"product_id": 15, "quantity": 1}]
I get product_ids [14,18,15] from…

Greedy Pointer
- 334
- 5
- 12
-1
votes
2 answers
Property [posts] does not exist on this collection instance laravel
I have three tables
Users
id name email
friends
id user_id friend_id
Posts
id image users_id
I want to show all posts of login user and of his all friends .
to get post of login user I done…

راجہ سفیان
- 27
- 1
- 6
-1
votes
3 answers
How to get tasks that match logged in user id?
In Controller:
$user = user()->id;
$tasks = task::with('user')->where('tasks.user_id', '=', '$user')->get();
Task Model:
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
User Model:
public function Tasks()
{
…

Laing
- 3
- 5
-1
votes
1 answer
How to calculate total price of multiple items in cart in Laravel?
I have a carts table where column is----> "id", "user_id", "product_id", "quantity". See the image
here is ther products table
When a user add item into cart ,I want to get total price of products which is added into cart by the user. How to do…

Prince
- 39
- 8
-1
votes
1 answer
weird behaviour or misunderstanding of laravel relations
weird behavior or misunderstanding of Laravel relations.
I have got a Product model. I getting products with translations and its category.
I have defined relations in both models.
I can access the data in the model or in the controller. but in view…

Murod
- 22
- 1
- 6