Official documentation: https://laravel.com/docs/7.x/eloquent-relationships
Questions tagged [eloquent-relationship]
944 questions
2
votes
2 answers
Eloquent: Get 'max' values from a `hasMany` -> `hasOne` relationship
I have an interesting eloquent challenge and my knowledge is not quite there to work it through, hoping you smart folks can give me some guidance. I have read this: Getting just the latest value on a joined table with Eloquent and it is very close…

emd
- 434
- 1
- 7
- 18
2
votes
0 answers
How to get data from pivot table which has ids from three tables
I have three tables call Users, Services, Categories and pivot table call professionals.
Also four models for all tables.
User: Id, name
Services: id, service_name
Categories: id, service_id, category_name
Professionals: id, user_id, service_id,…

ra.emi
- 31
- 2
2
votes
1 answer
Laravel Converting Existing User System Multi-Type
I recently took over a project that has an existing user system in place (standard Laravel 5.7 make:auth setup). What I've come to identify is that there is a significant amount of MySQL database columns within the user table that are blank or NULL…

radiantstatic
- 342
- 4
- 20
2
votes
3 answers
Adding filter to eloquent resource to attach relationship conditionally
Laravel 5.8
PHP 7.4
I want to load the relationships conditionally like
http://127.0.0.1:8000/api/posts
and
http://127.0.0.1:8000/api/posts/1 are my end points now, I want to load comments like
http://127.0.0.1:8000/api/posts/?include=comments…

Prafulla Kumar Sahu
- 9,321
- 11
- 68
- 105
2
votes
1 answer
Laravel - How to access related object other relashionship
I have 3 Models "Category", "Post", and "User".
A Category has a hasMany relationship with Post. And a Post has a belongsTo relationship with User.
I have a Category object $cat1 and i can access its posts (and the user_id) in my view, but i can't…

Badr
- 177
- 4
- 17
2
votes
5 answers
Laravel Nova - How to hide 'Create' button from HasMany field?
I've User model which has HasMany relation with Post model. When I include a field for HasMany in User resource of Nova, I see there is Create post button. How do I remove/hide that button?

Gaurav Deshpande
- 31
- 1
- 4
2
votes
3 answers
How to join 3 tables with Laravel's Eloquent relationships with Eager Loading?
So, an order has a foreign_key offer_id.
And an offer has a foreign_key item_id.
An item may be in multiple offers. But every offer has one item.
An offer may be in multiple orders. But every order has one offer.
When I do this:
$orders =…

padawanTony
- 1,348
- 2
- 22
- 41
2
votes
1 answer
How to eloquent relationship with multiple results
Problem
I have two classes, Users & Posts. A user "hasMany" posts and a post "belongTo" a user. But when I call "User::all()" it doesn't automatically pull the users posts for obvious reasons, because if my user had relations to 100 different tables…

Kenziiee Flavius
- 1,918
- 4
- 25
- 57
2
votes
2 answers
Sort by relationship first in laravel
I have two tables: admins and log_doctor_infos. admins table has relationship hasOne with log_doctor_infos throught doctor_id like this.
In model Admin:
public function logDoctorInfo() {
return $this->hasOne(LogDoctorInfo::class, 'doctor_id',…

Ngoc Tran
- 69
- 2
- 12
1
vote
2 answers
Laravel Eloquent Where sum of relationship column is more than parent value
I'm trying to write a query scope in Laravel Eloquent to get records only where the sum of a records relationship values is more than a value stored in a parent column. So for example I have:
Table A
id
total
1
50
2
100
Table…

Matdragon
- 169
- 1
- 7
1
vote
1 answer
How to Link Related Records (Single Model) in Laravel
Similar to linking issues in Jira or work items in ADO. What's the best way (in Laravel) to go about saying tickets 1, 2, 3, 4, and 9 are all "related" to each other and no matter which record I'm looking at I see the full list of related records. …

Charlie Grove
- 21
- 3
1
vote
1 answer
in laravel 10 eloquent hasMany relationship I would like to add a "where like" clause
I am creating an interactive text form input using Livewire.
When the user types some characters into the text box I wish to return the filtered registrations from a table that has a hasMany relationship to the user.
In essence, the user hasMany…

DeveloperChris
- 3,412
- 2
- 24
- 39
1
vote
1 answer
How to split Laravel query result at the end after doing some common query at beginning?
I want to split a Laravel query result at the end of the query after doing some common query in the beginning. Scenario, code smple and my try is given below.
Code sample
$bankSlipIds = [1,2,3];
$cashSlipIds = [4,5];
$sourceIds = [1, 2];
//common…

Md. Amin Hossain
- 58
- 1
- 8
1
vote
1 answer
With Method in Laravel eloquent not work, Using PgSql
I am Laravel Eloquent ORM using PgSql, when i use with() method is return error.
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: uuid = integer LINE 1:
Laravel Version 10.7.1
Php Version 8.1.17
Here is the table structure,…

Zulfikar
- 49
- 6
1
vote
1 answer
Getting field value from another table returns a json collection in Laravel
I am using Laravel orchid package and in layout file, I have this function:
protected function columns(): iterable
{
$org = '';
$employeeDetails = '';
return [
TD::make('Userid', __('User Id'))
…

samir chauhan
- 1,543
- 1
- 17
- 42