Official documentation: https://laravel.com/docs/7.x/eloquent-relationships
Questions tagged [eloquent-relationship]
944 questions
0
votes
1 answer
Paginate eager loaded relationships results
i am trying to paginate a relationship result set by using a code very similar to this
with([
'secondaryMorph' =>…

Renoir Reis
- 359
- 4
- 17
0
votes
2 answers
Can I send a variable into laravel model?
So I'm trying to add get a specific data from a related table using the below method, but I don't know if that is the correct way to do it. here is what it looks like.
public function transspecific($lid){
return…

Agil
- 376
- 5
- 26
0
votes
3 answers
How to join two or more table using laravel eloquent method?
Little confused about eloquent-relationship joins, since I used to get the result by query builder so far. Referred with other related questions still, I am not clear. Please explain me with a better example.
Model 1 - customer
Model 2 -…

OutForCode
- 381
- 9
- 26
0
votes
1 answer
Laravel 5.7 - Get all users from current user groups
This this the structure that I currently use
Model User :
class User extends Authenticatable implements MustVerifyEmail
public function groups()
{
return $this->belongsToMany('App\Group')
->withTimestamps();
}
Model Group :
class…

mchev
- 713
- 1
- 8
- 22
0
votes
1 answer
How to get many-to-many relationship data using pivot table
I have below models:
1. Campaign
2. Coupon
3. CampaignCoupon
CampaignCoupon is model of pivot table for many to many relationship of Campaign and Coupon.
I have below relationship in CampaignCoupon model:
public function coupons() {
return…

Palak Jadav
- 1,202
- 1
- 11
- 23
0
votes
0 answers
how to join second and third table together in eloquent
i have three model 1.Document 2.Sharedoc 3.User
Document.php
class Document extends Model
{
public function sharedoc(){
return $this->hasMany('App\Sharedoc','docId','id');
}
public function user(){
return…

Fahime Ghorbani
- 31
- 1
0
votes
1 answer
laravel join with polymorphic pivot table using in on clause model names
I'm using eloquent polymorphic relationships that is awesome to manage pivot table between diferent models and other model called company.
I have a pivot table that contains the following structure:
I have to make a join query between vehicle…

bcg
- 261
- 1
- 4
- 26
0
votes
2 answers
Laravel Eloquent BelongTo Model Access fails
I am trying to get data by using Laravel Eloquent HasMany (reverse) relationship but I am not getting access. Whenever I try, it shows Trying to get property 'name' of non-object
I have two models. Category and Article. Category hasMany Articles.…

Mosharof Zitu
- 43
- 1
- 13
0
votes
1 answer
Syncing Many to many relationship in Event Listener (Laravel 5.7)
Laravel App has roles and each Role has permissions (many to many)
Each user has multiple roles (Many to many)
When the permissions of Role are changed/updated, Event RolePermissionsChanged is triggered and the listener has following code:
public…

Curated Prime
- 47
- 4
0
votes
1 answer
Cannot using where in eloquent
I'm using Eloquent ORM in Laravel. There is 2 model, User and Trans. User hasMany Trans, and Trans belongsTo User. The problem is when I'm using query where, it doesn't work.
I tried using ->get() in the last code, it still doesn't work. I tried…

Ray Coder
- 1,022
- 3
- 15
- 30
0
votes
1 answer
Laravel 5.7 Eloquent Relation for listing
i am trying to get dataset with join, and i wanna do it using eloquent and automatically without no manual query. I did the relations too but when i try to list the data joined, somehow it does it otherway around.
Laravel version 5.7
MySQL…

Skywarth
- 623
- 2
- 9
- 26
0
votes
2 answers
Insert/create data using belongsTo Relation in laravel
Phone BelongsTo User. Now how i insert phone & user data both through belongsTo Relationship & auto get user_id foreign key in phone table. Here I try this code but not work.
$phoneData = $request->get('phone');
$userData =…

Suvro Hossain
- 1
- 2
- 4
0
votes
1 answer
Laravel Eloquent for hasMany relation cannot find records
I can not find why it does not work.
In my Course model I have defined relation:
class Course extends Model {
public function courseDates() {
return $this->hasMany(CourseDate::class, 'course_id');
}
}
And in my CourseDate model…

Michal Palus
- 13
- 3
0
votes
1 answer
Complex relationship formation in Laravel 5
I'm working on a complex shopping cart project. I have relationships like this
CategoryGroup model
// App\CategoryGroup
class CategoryGroup extend Model
{
public function categories()
{
return $this->hasMany(Category::class);
…

Munna Khan
- 1,902
- 1
- 18
- 24
0
votes
2 answers
how to get objects from relation of relation in laravel
I have 3 model: Course,group,student
I want to give games from Course with 2 relations.
for ex: I want students of Course 1 (id=1)
The Course has Many group(5,6,8) and each group have One(36,38) or Many(35,37) students
How to get all students of…

Mohammad Hosseini
- 1,649
- 1
- 16
- 31