Official documentation: https://laravel.com/docs/7.x/eloquent-relationships
Questions tagged [eloquent-relationship]
944 questions
1
vote
1 answer
Cannot use belongsToMany to sync pivot data
I have a pivot table called invite_riskarea_riskfield which defined a relationship with the invite table:
What I need to do is sync multiple invite_riskarea_riskfield permissions (insert, edit, view). So I tried to set the following relationship in…

sfarzoso
- 1,356
- 2
- 24
- 65
1
vote
1 answer
Null Coalescing Operator not working in laravel eloquent
Controller
Tag::with('tagTranslation')->find(5)
Tag Model
function tagTranslation()
{
$locale = Session::get('locale'); // $locale = 'bn';
return $this->hasOne(TagTranslation::class, 'tag_id')
->where('locale', $locale)
…

Irfan Chowdhury
- 11
- 2
1
vote
0 answers
Laravel multiple polymorphic relationship
I have a database tables as followed
Posts Table
Comments Table
Users Table
Pages Table
Likables…

drhtoo
- 11
- 2
1
vote
2 answers
How to access Inverse Model Relationship when printing data in foreach loop at Blade
Lets say I have an Attribute Model like this:
class Attribute extends Model
{
public function group()
{
return $this->belongsTo(Group::class);
}
}
And a Group Model like this:
class Group extends Model
{
public function…

Pouya
- 117
- 1
- 14
1
vote
3 answers
I want to access the products with total amount
I have 2 tables. One is product(id, name). another is In(id, amount, products_id).
I am trying to get product name with total amount.
Example:
Aerobat-34
God of war-3
Rs537-15
I Joined the tables. Now output…

Md Mredul Sarker
- 13
- 3
1
vote
1 answer
belongsToMany doesn't return the expected query
In my application I have a table called users, each user can have one or multiple roles (I'm using laratrust), in this scenario I will cover the tenant and the patient role.
The package santigator/laratrust handle the role within this…

sfarzoso
- 1,356
- 2
- 24
- 65
1
vote
2 answers
Laravel Single User Data In A Multichained Distant Relationship
I have three models as per the code below.I have a problem of returning a single savings data for authenticated users. Any help will be highly appreciated.
I want a low like this:
$savings =Savings::client()->client_users()->where('user_id', '=',…

Kevin Otieno
- 55
- 1
- 10
1
vote
2 answers
How to send relational data from multiple foreach to view in Laravel
I made a Laravel project where Category, Subcategory and Child category working using same table relation.
I want to show all products from childcategory when click to Main Category.
So for that I need to use multiple foreach loop like this,
foreach…

Abir Husain
- 57
- 5
1
vote
2 answers
Laravel eager loading with where() from its relation
I have 3 tables that have relations
Budgets Table :
Field
Type
Null
Key
Default
Extra
id
bigint(20) unsigned
NO
PRI
NULL
auto_increment
user_id
bigint(20)…

owf
- 245
- 1
- 9
- 26
1
vote
0 answers
Relationship is returning null even when from the database the data exists
I have been getting an empty array when I query this model relationship for data. I have tried pulling all the data associated with the model from the database and I still do not get the data even though physically the records are in the database.…

Favour Max-Oti
- 11
- 2
1
vote
0 answers
Is there a way to preform a belongsToThrough (and is it possible to sort on it)?
Given the following example:
Post
- id
- name
- user_id (FK to class User)
User
- id
- name
- rank_id (FK to class Rank)
Rank
- id
- name
Is it possible to make a query with an orderBy on user.rank.name?
$posts = Post::query()
->with([
…

Roel Pennings
- 11
- 1
1
vote
3 answers
Laravel Model relationship "Page have many Attachments"
I have models:
Page:
id
slug
Image
id
file
Video
id
file
I need the Page model to have a relation with several Image and Video models through one relationship, like
foreach($page->attachments as $attachment)
{
// $attachment can be Image…

Kekior
- 11
- 1
1
vote
0 answers
Laravel Eloquent: Relationship - hasOneThrough
I have three tables in my database:
inquiry
post
product
Columns in inquiry table:
id
message
post
Columns in post table:
id
title
content
product
Columns in product table:
id
name
I have an Inquiry model and I want to show the post title…

Dev2
- 11
- 1
1
vote
1 answer
Laravel 8 multiple models to single view Error $address Not defined
I'm trying to create 2 rows in the DB using findOrNew() but when I use the ID from the Users model to create another model(Address) the programs returns undefined variable $address. I don't know if I'm using the correct approach or not. Bellow you…

ismael620
- 17
- 4
1
vote
1 answer
How to join 3 table eloquent laravel 8
i have 3 tables
user
user_id
name_user
contacts
request_id
request_register
request_id
user_id
team_id
team
team_id
name_team
i want get list of user details who register to team with spesific id, example:
team with id…

Ihda Anwari
- 25
- 1
- 7