Official documentation: https://laravel.com/docs/7.x/eloquent-relationships
Questions tagged [eloquent-relationship]
944 questions
1
vote
1 answer
Unable to fetch data from Laravel Eloquent Relationships
i wanted access the value of content_type_id .i tried like $courseChapters['chapter_content']['content_type_id'] , $courseChapters['content_type_id'] , $courseChapters[0]['content_type_id'] & $courseChapters['chapterContent']['content_type_id'].All…
user17189691
1
vote
1 answer
Unable to get data on joining Multiple Eloquent Relationships
i wanted to display id,FileName & FilePath from Files table and id & name from User table along with the course table columns - id,courseDisplayName & aboutCourse. But it is returning null from both files & user relations.How can i fix…
user17189691
1
vote
1 answer
Laravel Eager Loading removing empty values on children
I have this eager loaded relationship:
$companies = Company::with('employees.records')->get();
If an employee has no record, I would not like to include it in the result. Here's my final query:
$companies = Company::whereIn('name', ['A',…

user3233787
- 379
- 1
- 10
- 32
1
vote
2 answers
Laravel: How to query a Model Relationship only if it exists
I have a model Survey with a column installer_id which is related to hasOne another model Installation which is related to hasMany another model Assignment.
I want to write a query to fetch all Survey where the installer_id is not null, and where…

nellytadi
- 91
- 1
- 15
1
vote
0 answers
Laravel resource whenLoaded hasManyTrough relation
I am trying to build a resource collection with a second resource based on a hasManyThrough relationship. When i call the actual relationship on the main resource it does provide me the correct data. Whenever i pass the data to another resource and…

Jesse de Boer
- 92
- 5
1
vote
0 answers
Get aggregated information as part of an Eloquent many-to-many relationship
I am new to Laravel and need some help with what might be a basic question.
Race horses (can) have many owners. Owners (can) have many race horses. However, owners can buy and sell shares in race horses. At any point in time it must be possible to…

Philip
- 3,689
- 3
- 24
- 35
1
vote
1 answer
Call loadMissing() in a isolated model replica
I have a question maybe a little silly. I'm trying to replicate and store a model instance in a variable to call the loadMissing method. The idea is to call the loadMissing method without being affected by the original model. Let me explain.
I have…

leo95batista
- 699
- 9
- 27
1
vote
1 answer
How to returning only as string column in json response object from eloquent relationship using eager loading
I'm trying to figure out how to eager load data as a string instead of array object from a related table.
I have 3 models and here are the relations
Product.php
class Product extends Model
{
public function ProductTag()
{
…

Marty
- 302
- 4
- 18
1
vote
1 answer
is it possible to return different models in an API response
This is my first time posting here so please pardon my errors:
I have a search functionality whose route is:
Route::get('/search', 'SearchController@index');
Currently, I have an eloquent relationship where products has many deals. is it possible…

Evidence Ekanem
- 101
- 1
- 8
1
vote
1 answer
How do i resolve Call to a member function only() on array error
I want to use laravels FormRequest to validate before updating some fields. This works fine if i just use:
User::find($application->userid)->fill($request->only('first_name'...
but the request also contains sub array…

jkstallard
- 375
- 1
- 2
- 17
1
vote
1 answer
How to get days in Eloquent Query In Laravel
I want to find days between 2 dates in Eloquent Query In Laravel.
I have Query Like this,
$credentials = Documents::whereIn('agency_id', $agency_ids)
->with('dp_credential')
->get()->toArray();
if (count($credentials)) {
foreach…

Ankita Dobariya
- 823
- 5
- 14
1
vote
0 answers
Laravel: get attributes of related polymorphic model (morphOne)
Let's assume I've got a User model ("users" table => id, email, userable_id, userable_type) and a Customer model ("customers" table => id, name etc., not important fields).
User model has the following:
public function userable()
{
return…

Kay
- 61
- 3
1
vote
1 answer
Laravel: How do i return data combinely when one table is related to more than one table?
So, in my application I have models: user, category, service(i.e subcategory) and userService.
There exists many to many relationship between user and category, user and service. And one to many between category and service.
// Tables
Users :…

Milan Budhathoki
- 61
- 2
- 3
- 7
1
vote
1 answer
Laravel Eloquent belongsToMany only fetches one result
My belongsToMany function only returns a single element when it should return all of them.
This is my table structure (simplified):
users
id
...
groups
id
...
users_in_groups
id
user_id
group_id
This is my function:
public function…

mapawa
- 179
- 1
- 4
- 16
1
vote
1 answer
laravel eloquent update hasmany with array of ids
I need to update HasMany relation with set of id's.
Faq model.
class Faq extends Model
{
public function products(){
return $this->belongsToMany(Product::class, 'faq_products');
}
}
I have Product model and pivot table 'faq_products'…

vimuth
- 5,064
- 33
- 79
- 116