Official documentation: https://laravel.com/docs/7.x/eloquent-relationships
Questions tagged [eloquent-relationship]
944 questions
1
vote
2 answers
Laravel conditional eager loading
I'm developing an application where a User can be of type Admin, Client, Supplier or Manager. Each type (except Admin) has it's own model with respective data, and related with a user_id. For example:
users
- id
- name
- email
-…

RBFraphael
- 367
- 1
- 3
- 13
1
vote
3 answers
How to change laravel eloquent relationship property without affecting another entry's relationship property
How can I change the property that belongs to one of returned entry's relationship without affecting the other?
The example query below returns two entries with each having their own set of relationship attached to them
$tttt =…

Yeo Bryan
- 331
- 4
- 24
1
vote
1 answer
Update existing foreign key values using eloquent
I am trying to update multiple rows of data with existing foreign key values where basket should only link to the foreign ID of 100.
Example:
id
title
basket_id (foreign key to another table)
1
apple
100
2
banana
200
2
kiwi
300
What…

cafe123
- 55
- 6
1
vote
1 answer
How to get the topics that have most answers by Eloquent Relationship
I'm working on a forum project using Laravel 9 and for this project, there is a One To Many relationship between Question & Answer:
Question.php Model:
public function answers()
{
return $this->hasMany(Answer::class,'ans_que_id');
…

Pouya
- 114
- 1
- 8
- 36
1
vote
1 answer
Laravel get related columns with custom query
So I have a function to get all companies in range based on longitude and latitude. Now I want to get te related columns with the query, but I do not know how.
This is the code how I am getting the companies in the area:
$companies =…

Moemen Hussein
- 394
- 5
- 16
1
vote
1 answer
Laravel Many To Many (Polymorphic) issue
I've been trying to create a Many To Many (Polymorphic) system that saves the state of every update done by a specific user on specific Models (for instance: Company, Address, Language models) with a note field and the updated_by. The goal is to…

McRui
- 1,879
- 3
- 20
- 31
1
vote
0 answers
ELOQUENT LARAVEL Cast integer to string column value in hasOne relationship
I want create one-to-one relationship into two tables.
One of my tables have column typed in int but the second have foreign key column typed in string.
Into database :
user table:
id: int
life table:
user_id: string
in User model :
public…

Valentin Leguy
- 97
- 1
- 7
1
vote
1 answer
How to pass all categories and subcategories to layout laravel 9
I'm new to development, I ran into a problem, I can't display all subcategories in the "layout.app" For each category, only one subcategory is displayed, and I need to get all subcategories
in file AppServiceProvider.php
public function boot()
{
…

Javlon Sattarov
- 13
- 4
1
vote
0 answers
Laravel Model getRelation returns all rows
I got a User Class
class User extends Model implements
AuthenticatableContract,
CanResetPasswordContract,
AuthorizableContract,
MustVerifyEmailContract {
//use HasApiTokens, HasFactory, Notifiable, MustVerifyEmail;
use…

V1337
- 13
- 1
- 6
1
vote
1 answer
How to use an array as a foreign key in a model and build a hasMany relationship in laravel
This is my schema as follows:
Medicines
id
price
description
etc..
Ecommerce Orders(has many Medicines)
id
user_id
products (array that holds the id's of the medicine's and quantity)
And in my EcommerceOrders Model i have
protected $casts = [
…

cdBreak
- 63
- 6
1
vote
1 answer
How to sort parent data based on child column detial in laravel 5.4?
I have 3 way relationship firstl i have get code like this in a controller
Trial::with('subjects')->where('source_id', $sourceId)->get()->toArray()
Now I want to sort subject.reactions on desc order of subject.reactions.accounts.nb_followers…

Bhimani Rutvik
- 398
- 2
- 13
1
vote
1 answer
What is difference between Eloquent's Model::query()->get() and Model::get()?
I want to know that, what is difference between Eloquent's Model::query()->get() and Model::get()?
Model::get();
and
Model::query()->get();
I want to know that, what is the benefit to use query() method?

AR-Shahin
- 11
- 2
1
vote
0 answers
Laravel Eloquent Relations has one through
I have 3 important tables
#Vehicles ( Model Vehicle )
- Id
- Name
- Description
#FilterOptions ( Model FilterOptions )
- Id
- GroupId
- Name
- Value
#VehicleTags ( Model VehicleTag )
- Id
- fId (Is linked to…

w3_
- 64
- 1
- 1
- 14
1
vote
3 answers
Laravel API: Load nested relationship in resource
I've got 3 models: Order, OrderItem and Product (for simplicity just showing the relationships):
class Order extends BaseModel
{
use Uuid;
protected $casts = [
'status' => OrderStatuses::class,
];
/**
* An Order has…

MrCujo
- 1,218
- 3
- 31
- 56
1
vote
2 answers
How to get a value from a three-level depth relation in Laravel using Eloquent?
I have 3 tables as below:
Users Table (has only one phone):
id
Name
phone_id
1
Sam
1
2
Tom
2
/**
* @return HasOne
*/
public function phone()
{
return $this->hasOne(phones::class);
}
/**
* @return ???
*/
public function key()
{
…

buczo
- 15
- 5