Questions tagged [eloquent-relationship]

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

944 questions
3
votes
1 answer

How to get All Child Category Product under Parent Category id in laravel

In categories table categories id categoryName parentId 1 Men's Fashion NULL 2 T-Shirt 1 3 Pants 1 4 Shoes 1 And another table is products id productName productCategoryId 1 A 2 2 B 3 3 C 4 And My Menu List…
3
votes
1 answer

Updating laravel relationship data

When we inserting or updating the data via Eloquent relationship model, which is the best approach to use? Example $user->profile->update(['salary' => 5000]); vs $user->profile()->update(['salary' => 5000]); I understand that $user->profile()…
cww
  • 593
  • 1
  • 8
  • 16
3
votes
2 answers

Laravel: sort query results based on field of nested relationship

I have two models with relations as defined below Order public function owner() { return $this->belongsTo(User::class, 'owner_id'); } User public function company(){ return $this->belongsTo(Company::class, 'company_id'); } company table…
3
votes
1 answer

Laravel HasMany with where condition

I have two tables, users and products. Products table structure is: id product_name user_id isGlobal 1 apple 10 0 2 banana 10 1 3 pear 20 0 4 melon 30 0 Here is User model where made relation with products public function…
Hayrulla Melibayev
  • 462
  • 1
  • 5
  • 20
3
votes
1 answer

Laravel user hierarchy w/ relationship unit testing gone wrong

Scenario: So, I've got a users table that contains a ForeignKey named parent_id that references the id of the users table. This allows for one User to belongs to another User, and a User having Many "children" Users (one-to-many). Now, the question…
abr
  • 2,071
  • 22
  • 38
3
votes
1 answer

Remove unmatched criteria records from Eloquent collection

I have some problems with Eloquent Eager Loading. I have added whereHas to remove the blog don't meet the comment criteria but the comment stills return empty array. My intention is to completely remove it from the json record. How can I completely…
3
votes
2 answers

Laravel: save / attach / sync custom pivot model (belongsToMany)

hope you're having a good day. I'm using Laravel 8. I have three models and I need those models "entangled", so to speak. So, I have three basic tables areas,threats,positions --- id name So the relationship needed is something like this: Every t3…
chuysbz
  • 1,262
  • 6
  • 18
  • 47
3
votes
2 answers

Laravel filter nested relationship

So I have three models: Volunteer, Task and Payment. A Volunteer can have many (has many relationship) tasks and a task can have many (another has many relationship) payments. class Volunteer public function tasks() { return…
3
votes
2 answers

Laravel eloquent update column using relationship column

How can achieve this query? Sale::with(['catalog']) ->whereIn('id', $ids) ->update(['price' => DB::raw('catalog.price')]); This is not working, it shows undefined table... I tried to put the name of the table but it's the same. On the…
Marc Pont
  • 988
  • 2
  • 14
  • 34
3
votes
4 answers

I can't retrieve data from the related table(model) in Laravel6

I can't retrieve data from the related table. There are 3 models(tables). User Chirp (has 'user_id' as foreign key) Click (has 'chirp_id' as foreign key) then I want to retrieve User & Click's data from Chirp model. So I…
Tieria
  • 333
  • 3
  • 11
3
votes
0 answers

Eloquent Models inheritance problem (Customers, Suppliers, Employees)

I have some problemns modeling in laravel my web app. I have Customers, Suppliers, Employees model, and a model Person for the shared data. In my datata base i have the next structure Person (id, cif, name, email, identification_type) Customers (id,…
3
votes
1 answer

Laravel hasMany relationship select specific column issue

I have a relationship inside my laravel model /** * Relation with calculations table * * @return object */ public function calculations() { return $this->hasMany('App\Calculation'); } When i am selecting data with relation…
Rajat Sharma
  • 369
  • 2
  • 10
3
votes
1 answer

Laravel eloquent pagination call in relational database

I have an user. User create many ads.I want to see users details with ads where ads shown by paginate. For this, i make two model(User & Ads) public function user(){ return $this->hasOne(User::class, 'id', 'user_id'); } public function…
omor faruk
  • 81
  • 1
  • 7
3
votes
1 answer

Laravel: How to define belongsTo in a MorphPivot?

In my project there is a Many-to-Many polymorphic relationship (Many instances of a model called Package (morphedByMany) can contain many different content types (each MorphedToMany). I've created a pivot table containing some additional fields that…
3
votes
3 answers

Nested relationships with Laravel faker - laravel seeder

I wanted to make a post seeder with users and comments as mentioned in the relationship section in the laravel documentation https://laravel.com/docs/5.5/database-testing $users = factory(App\User::class, 3) ->create() ->each(function…
glupeksha
  • 460
  • 1
  • 6
  • 14
1
2
3
62 63