Official documentation: https://laravel.com/docs/7.x/eloquent-relationships
Questions tagged [eloquent-relationship]
944 questions
0
votes
2 answers
Combinations of filters can be optional laravel eloquent relationships
I want to filter replies based on query parameter passed, query parameters can be post_id, comment_id and array of reply_ids all can be optional and can be used as combination
$query = Post::where('user_id', auth()->id());
if (…

Prafulla Kumar Sahu
- 9,321
- 11
- 68
- 105
0
votes
2 answers
Create a new record in multiple tables with relationsship
I croak that there is something that escapes me.
I have a User model that has a hasMany relationship with another model called Domain
I thought it is possible to save the data in both tables in a single command but I see that it is not or…

abkrim
- 3,512
- 7
- 43
- 69
0
votes
1 answer
Adding a filter helper method to Laravel model
I'd like to add a method to my Laravel Product model which filters by name attr and returns a collection of all matching products, here's what I've got:
Product.php
public function filterByName($query)
{
return…

f7n
- 1,476
- 3
- 22
- 42
0
votes
3 answers
How to display eloquent query into view blade?
I am querying a list of staffs from 'itemregistration' table with the eloquent relationship with 'section' table. But I cannot display the information from section table to the view blade.
My controller get the query as follows:
$itemregistrations…

joun
- 656
- 1
- 8
- 25
0
votes
1 answer
how to get only active relationships in laravel eloquent in when
hi i have a when eloquent clause that i want to pick only the properties that has active relation of discount so my code is like below :
->when($has_discount, function ($query, $has_discount) {
$query->with([
…

Farshad
- 1,830
- 6
- 38
- 70
0
votes
1 answer
Is it correct to create different Api Resources for each request?
Will it be correct, if I create API RESOURCES for each request. And how to make a connection between three tables in Resources. For example:
class UserResource
public function toArray($request)
{
return [
'id'=>$this->id,
…

Aleks
- 147
- 10
0
votes
1 answer
Issue with seeder between 3 entities
I have an issue with the seeder news. It has a relationship between 2 entities. The result of the command php artisan db:seed is the next:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1364 Fi
eld 'user_id' doesn't have a…

carlos sierra garcia
- 47
- 2
- 7
0
votes
1 answer
Get raw sql query from belongsToMany relationship
How do I get raw sql like "SELECT * FROM table WHERE id = 1" from belongsToMany.
public function users(){
return $this->belongsToMany('App\User','user_projects','project_id','user_id');
}
i tryed dd()
tryed this:
$query =…

sby05922
- 35
- 6
0
votes
1 answer
Model return empty array when use select in model (Laravel)
I have a hasOne(Many) relation function like this:
return $this->hasOne('App\Models\ProductTranslation','product_id','id')->where('language_id', $language_id['id']);
Also, I tried to use
return…

Hik200
- 107
- 11
0
votes
1 answer
How can i Get Laravel relationship with its relationship values?
I have a laravel database design where every result has belongsToMany relationship with question and every question has belogsTo relationship with subject. i want to fetch subject names from results with single statement on laravel
I have tried this…

dsaha1656
- 3
- 5
0
votes
3 answers
how to send json object with arrays inside it by using laravel eloquent?other question for this is how to change json object to array?
i was returning data in json format from controller,using eloquent and it was working fine.At one location i need to implement a further condition on data so i implement where condition on elequent's retrieved data and then implement toJson() on it…

Rana Nadeem
- 1
- 2
0
votes
2 answers
Laravel Relationship between Three Models
sold_items:
id order_id customer_id name
-- -------- ----------- ----
1 5 14 An object
orders:
id po_num
-- ------
... ...
5 123
customers:
id name
-- ----
... ...
14 …

cyber-leech
- 37
- 2
- 2
- 9
0
votes
1 answer
Laravel Many-to-Many Relationship Field Names
I have a users table and a permissions table. One user can have many permissions, and one permission can have many users:
USER ID | PERMISSION ID
1 | 1
1 | 2
2 | 1
2 | 1
There is a linking table called permission_user as defined by the Laravel spec…

Josh Menzel
- 2,300
- 4
- 22
- 31
0
votes
2 answers
Laravel get exercises by day from pivot table
I am trying to query a pivot table to show how many exercises have one day, but there is something wrong with that.
I need to get all the exercises by Monday or any day.
I have three tables: routines, exercises and exercise_routine.
routines …

desancheztorres
- 353
- 1
- 6
- 13
0
votes
1 answer
Laravel - Eloquent Model Relationship with Order By Inner query is not working
here is my collection.
0 => array:11 [
"_id" => "5d6447ae1ab38901a80c7062"
"order" => 4 //order of out array
"widget_order" => array:6 [
"_id" => "5d6451281ab38901b4689d24"
"widget_id" => "5d6447ae1ab38901a80c7062"
"order" => 3 //order of…

rameezmeans
- 830
- 1
- 10
- 21