Official documentation: https://laravel.com/docs/7.x/eloquent-relationships
Questions tagged [eloquent-relationship]
944 questions
0
votes
1 answer
Eloquent 5.7 Relationships return empty
i am stuck at this point, i tested a lot of things and can't see what is happening, i need to create a reservation summary but i get empty data from the relationship
my tables are:
res_reservations key = idReservation
res_reservation_rooms key =…
0
votes
3 answers
How to define Laravel relationship when multiple columns are associated
I have models just like I have mentioned below,
How laravel eloquent-relationship can be implemented on it?
User Model
id
name
Post Model
id
title
desc
created_by = foreign key on User->id
Stage One…

Prafulla Kumar Sahu
- 9,321
- 11
- 68
- 105
0
votes
3 answers
Laravel eloquent model relationship
My app builds on
Laravel: 5.6.35
PHP 7.2.4
Entrust: 1.9
My Role model
class Role extends EntrustRole
{
public function permissions()
{
return $this->belongsToMany(Permission::class);
}
public function users()
{
…

Prafulla Kumar Sahu
- 9,321
- 11
- 68
- 105
0
votes
1 answer
Eloquent - How to use "Has Many Through" relation?
I have three tables - products, features and product_feature - as
products
- id
- name
features
- id
- key
product_feature
- product_id
- feature_id
- value
I was retrieve all (key, value) pairs for a product. The SQL…

Tushar Bhatt
- 15
- 1
- 11
-1
votes
1 answer
unable to GROUP_CONCAT for 'one to many relationship' when query has multiple other joins
I am unable to use GROUP_CONCAT for 'one to many relationship' when the query has multiple other joins.
This is the error message:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2
of SELECT list is not in GROUP BY clause and…

Paul Kendal
- 559
- 9
- 24
-1
votes
1 answer
Updating Relationships from OneToMany to ManyToMany in Laravel
Currently, I have Post model having these hasMany with inverse relationships assigned to it.
public function category()
{
return $this->belongsTo(Category::class);
}
public function group()
{
return $this->belongsTo(Group::class);
}
public…

JS TECH
- 1,556
- 2
- 11
- 27
-1
votes
1 answer
Laravel 8.83.27, Undefined property: stdClass::$image (view: /Application/../..)
I have two tables:
items
id
name
description
created_at
update_at
images
id
name
item_id
created_at
updated_at
I did build a one-to-one relationship between the Item model and Image model.
Item model

GAOUSSOU DIARRA
- 3
- 3
-1
votes
1 answer
Convert row SQL query to Laravel Eloquent Query
I need this query to be Laravel eloquent, and also, how can I join the above tables in one single eloquent relational query?
I have tried many examples to solve this query if any ideas on how to solve this please describe me in response.enter link…

Sahil Rana
- 84
- 6
-1
votes
1 answer
How to build a Laravel relationship based on custom attribute?
There is a Client Model and a Lead model:
class Client extends Model
{
protected $fillable = [
'fn',
'ln'
];
public function fullName()
{
return new Attribute(
get: fn () => $this->fn . ' ' .…

JanBoehmer
- 395
- 3
- 14
-1
votes
1 answer
Laravel Relationship Model nested relation table
I want to ask about the relationship model laravel
if I have a model table like this
Model A (one to one with b)
- Model B (one to many with c,g,h)
- Model C (one to many with d, e)
- Model D
-…

AdityaDees
- 1,022
- 18
- 39
-1
votes
1 answer
How to do multi-level seeding in by using Eloquent Relationships in Laravel?
Here is the table stucture
Table A
Table B has foreign key a_id
Table C has foreign key a_id and b_id
I am trying to Seed these three tables with Eloquent Relationships
Here is my code structure
class A extends Model
{
use HasFactory;
…

Ratul
- 43
- 2
- 8
-1
votes
2 answers
I want to select all Users which are not members of a Group with id=6
My tables
users
id
name
Cell 1
Cell 2
Cell 3
Cell 4
group
id
name
Cell 1
Cell 2
Cell 3
Cell 4
group_users
id
user_id
group_id
Cell 1
Cell 2
Cell 4
Cell 3
Cell 4
Cell 6
// All users which are members of…

Lindani Dube
- 1
- 3
-1
votes
1 answer
How to relation with two table when column value is a array in Laravel
I need all reports with all officers included in the participants column.
please check the attached image.
https://prnt.sc/CRoELD48J-L5
user7039160
-1
votes
1 answer
Laravel Eloquent returns elements related to the first document only, when using with() + take() methods
i'm trying to get user documents along with first three related meeting documents.
User::with(
[
'meetings' => function($query) {
$query->take(3);
}
]
)->get();
user-meeting relation in…

erbelion
- 44
- 6
-1
votes
2 answers
Laravel 7: Finding a relation between two dates
I'm trying to get the jobs relation between two dates by using the whereHas method however I keep getting an Integrity constraint violation and am not sure what is causing this issue.
I've tried using the with and where method, but they all return…

Salman
- 1,109
- 3
- 25
- 55