Questions tagged [laravel-relations]
165 questions
1
vote
0 answers
Laravel Many to one in Resource
I use laravel 8 & have 3 table:
Products, ProductPrice & ProductsPublisher:
this is my Products model for this relationship:
public function lastPrice(){
return…

Soroush Tayyebi
- 137
- 1
- 9
1
vote
2 answers
I have a belongs to relationship in laravel 8 and it returns null when I am trying to use that relationship in the tinker shell
I am using Laravel 8 and I have the following very simple models and migrations,
Author Model

gfit21x
- 141
- 9
1
vote
1 answer
How to import excel file with relationship id?
I trying to import the lines of the job request by using an Excel file but I don't know how to store the id of the job request in the lines of it.
I have 2 models that are
Job Request
protected $table = "jobs";
protected $fillable=['job_id',…

Jill
- 49
- 6
1
vote
1 answer
Laravel: Use sync or updateOrCreate for updating parent table and child table?
I have a parent table like this:
clubs
id
name
budget
1
Arsenal
90
2
Chelsea
150
3
Man City
135
4
Man Utd
140
5
Tottenham
87
And a child table like…

Padawan
- 103
- 1
- 7
1
vote
1 answer
Laravel Relationship Query to load hasMany + BelongsToMany
I need to get brands data in specific collection page but only has more than 1 product.
Here are relations between models.
Brand -> HasMany -> Product
Product <= BelongsToMany => Collection
I was able to get brands data that have more than 1…

LoveCoding
- 1,121
- 2
- 12
- 33
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
0 answers
many-to-many in Laravel and select data by condition of relation
I have next tables in my Laravel…

Andrey Grytsenko
- 21
- 2
1
vote
1 answer
Laravel 5.8: Column not found: 1054 Unknown column error in Many To Many relationship
I have a Many To Many relationship between User Model & Wallet Model:
Wallet.php:
public function users()
{
return $this->belongsToMany(User::class);
}
And User.php:
public function wallets()
{
return…
user16333885
1
vote
1 answer
laravel Eager Load and limit
I meet this problem: I have model of people and model of their activities and I want to declare relation like latest activity inside of person model but Laravel does not give permission to use 'Limit' or 'Take' inside of Eager Loads
So how it could…

Hyzyr
- 568
- 4
- 13
1
vote
3 answers
How to get data from three related table with it's model relationships?
I have three tables like these:
Expense:-
expense_id
user_id
User:-
user_id
employee_id
Employee:-
employee_id
first_name
last_name
I want to get first_name and last_name from the employee table where expense.user_id = user.user_id, I tried…

Zia Yamin
- 942
- 2
- 10
- 34
1
vote
0 answers
Laravel Eloquent with() relationships - order by 2 different nested relationships
I wish to orderBy (sortBy) two different nested relationship columns in laravel.
I have the following query which returns the data that i need
return Assignments::with('worker.person', 'order_job.order.company', 'worker.rel_phones',…

00_adam
- 11
- 1
- 3
1
vote
1 answer
MySQL type CHAR problem in Laravel Eloquent
They say, if you want to find the answer, ask the right question. Really, here I don't know what to ask. Because I don't know what's happening.
Migration:create_table1_table
$table->char('code', 1)->primary();
$table->string('name', 50);
I seeded…

Aaron
- 138
- 1
- 7
1
vote
0 answers
Laravel 8 retrieving data from multiple models using with() returning empty arrays for related models
I'm using laravel 8, trying to retrieve data from related models which returns empty arrays except for the parent model.
I have 4 models
Product (Parent)
Product_categories
Product_attributes
Product_photos
I'm fetching the data like…

Sarath
- 41
- 4
1
vote
1 answer
Laravel - Self referential relationship does not work
I work with Laravel 8
In my database I have a Menu table with this data:
id | id_parent | name | route | routename | term |…

Jonathan Sigg
- 306
- 3
- 12
1
vote
1 answer
How to define multiple belongsTo in laravel
My table has many foreign key for example prefecture_id, gender_id and status_id.
And I made model for those table.
So I want to define multiple belongsTo method like following for get all data with query builder..
But In fact belongsTo can't use…

happy Coyote
- 55
- 9