Questions tagged [eloquent-relationship]

Official documentation: https://laravel.com/docs/7.x/eloquent-relationships

944 questions
1
vote
0 answers

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…
Lloyd Banks
  • 35,740
  • 58
  • 156
  • 248
1
vote
2 answers

How to add extra column to pivot table many to many

I have simple items table. Example: Paper. Made from bamboo and cotton (if google does not lie). Also, a book made from paper. So paper parent of bamboo and cotton also child of book. I think I can explain my table structure in two words. And now…
Randall
  • 2,414
  • 3
  • 35
  • 63
1
vote
1 answer

Laravel Eloquent appending a non-relation ship data with the result

I have this model file:- namespace App; use Illuminate\Database\Eloquent\Model; class Observation extends Model { protected $fillable = ['observation', 'recommendation', 'priority', 'report_asset_id']; …
Q8root
  • 1,243
  • 3
  • 25
  • 48
1
vote
1 answer

Laravel SUM One To Many (Inverse) / Belongs To Relationship

I have stuffs table where will be saved only stuff title. Stuffs table: id title Also I've user_stuffs table where each user has n stuff volume. User stuffs table: id stuff_id user_id volume How I can SUM each stuffs volumes and get like this…
Andreas Hunter
  • 4,504
  • 11
  • 65
  • 125
1
vote
2 answers

Can't insert into pivot table

I have a pivot table in my database that combines users and loans. The table is the following. user_id loan_id amount Now I have the following code to insert data into the database $user = User::find(2); $user->loans()->create([ …
Pranta
  • 2,928
  • 7
  • 24
  • 37
1
vote
2 answers

How to define the following relationship

I am developing a p2p app with laravel. I have two tables namely users and loans. Firstly, a user can be a lender or borrower. Then users can have multiple loans and multiple loans belong to multiple users. Also, a loan can belong to one borrower…
Pranta
  • 2,928
  • 7
  • 24
  • 37
1
vote
2 answers

Laravel Backpack - Inline create, relationship not being added on DB

I'm trying the 4.1 new feature "Inline create", but I can't seem to associate the ids of the items created. Let me explain what I'm doing / what I want: I have "Folders" that have "Chapters" inside (so 1-n relation). My code: CRUD::addField([ …
1
vote
1 answer

How can i use related model scope inside whereHas Laravel 8

I have two models. Task and TaskCheck in TaskCheck i have class TaskCheck extends Model { public function task(): BelongsTo { return $this->belongsTo(Task::class); } public function owner(): BelongsTo { return…
Drobek
  • 90
  • 1
  • 7
1
vote
1 answer

Nested Eloquent Relation - HasOneThrough Issue

I have below setup for tables: Product_variant Table Product_variant -> id, name, code, image Warehouse table Warehouse -> id, name, address Product Variant Stock table Product_Variant_stock -> stock_id, warehouse_id, variant_id, stock_qty Now,…
Gags
  • 3,759
  • 8
  • 49
  • 96
1
vote
1 answer

Trying to define a many-to-many relation with a pivot model in Laravel 8

Trying this for hours now and I don't see the error. I have a model 'User' and a model 'Round'. I want to define a n:m-relation with a model 'Flight' as pivot model. User.php
1
vote
1 answer

Eloquent recursive self join in addition to another table join

Frist, sorry for the not so clear title, I hope I can explain it better here: I have a MenuItem model which has recursive self join to represent multilevel menu. In addition, the menu item actually might have a foreign key to Post model. The model…
Lamar
  • 1,761
  • 4
  • 24
  • 50
1
vote
0 answers

Empty table after seeding in laravel8?

I just started working with Laravel and i'm trying to seed my DB. I have a many to many relationship between the users and shops table. When i'm running php artisan DB:seed the seeder for shops isn't running. And if i run php artisan db:seed…
Abdo Rabah
  • 1,670
  • 2
  • 15
  • 31
1
vote
1 answer

Laravel 8 Foreign Key relationship in eloquent issue

I have very simple 2 tables 1. products (id,name) 2. adjustment (id,product_id,amount) All tables have foreign keys and models are define as bellow class Product model have public function adjustment() { return…
Mr.SH
  • 397
  • 1
  • 9
  • 27
1
vote
0 answers

Fetching distinct or unique records from many to many relation in Laravel

I'm trying to fetch relational count which is in many-to-many relation in my Laravel 7.30 application. I've a many-to-many relational table named project_associate_brand which has project_id, product_group_id, product_subgroup_id and brand_id, so my…
mayuri
  • 31
  • 1
1
vote
2 answers

Laravel leftJoin only last record of right table and order by it

I have two tables. 1) products 2) prices ------------------------- - products - ------------------------- - id | int - - name | varchar - - created_at | datetime - - updated_at | datetime…