Questions tagged [laravel-relations]
165 questions
6
votes
1 answer
Laravel - Query "with"
I have a query where I need to get info from 2 tables. So I need to get invoices with the invoice_status 1, where the user_status is 1.
I though I could query the Invoices table with the users table. When I dd($invoices) I do get the invoice with…

Northify
- 351
- 2
- 5
- 21
5
votes
1 answer
Laravel Many to Many Sync with additional column
Laravel version 7.0
I have Team model and User model, team_has_users table.
team_has_users table has team_id, user_id, role columns.
One user can belong to one team with different roles.
For instance, one user can belong to one team as a client and…

LoveCoding
- 1,121
- 2
- 12
- 33
4
votes
1 answer
Laravel Eager Load with conditional statement
good day everyone
I'm new to Laravel and currently building my first simple news app with it
I have a simple query that using laravel's eager loading function
But in this eager loading function, I want to fetch certain comments based on some…

Gwein
- 109
- 3
- 14
3
votes
2 answers
LatestOfMany() of BelongsToMany() relationship
I've been using latestOfmany() for my hasMany() relation to define them as hasOne() for quite a while now. Lately I've been in need of the similar application but for belongsToMany() relationships. Laravel doesn't have this feature unfortunately.
My…

Skywarth
- 623
- 2
- 9
- 26
3
votes
3 answers
How to set a One To Many relationship in ONE model between different types
I have a table named categories and here is it's structure:
id bigint(20) AUTO_INCREMENT
name varchar(255)
description varchar(255)
short_name varchar(255)
picture varchar(255) …

Pouya
- 117
- 1
- 14
3
votes
4 answers
How to create a correct ManyToMany relationship between a user and a product in Laravel
I'm working on an Online Store project with Laravel 5.8 and in this project, I wanted to add "Add to favourites" ability for users to add a product to their favourite list.
So I created a Model like this:
class FavouriteProduct extends Model
{
…

yetep93258
- 33
- 1
- 13
3
votes
1 answer
How to synchronise child table data on hasMany relationship?
I have 2 table products and variants, with a hasMany relationship, the structure is below :
Product Model :
public function variants()
{
return $this->hasMany(Variant::class);
}
Variant Model :
public function product()
{
return…
user13672643
3
votes
1 answer
Laravel | PHP Array Recursive Merge with preserving key
I have three arrays.
$data1 = []; $data2 =[]; $data3 = [];
foreach($request->clients as $client)
{
$data1[]= [$client=>['role'=>'client']];
}
foreach($request->employees as $employee)
{
$data2[]=…

LoveCoding
- 1,121
- 2
- 12
- 33
3
votes
1 answer
Laravel MorphToMany Doesn't Work for multi columns
Laravel version: 7.0 Here is my table.
Schema::create('model_email_form', function (Blueprint $table) {
$table->id();
$table->string('model_type');
$table->unsignedBigInteger('model_id');
$table->unsignedBigInteger('email_id');
…

LoveCoding
- 1,121
- 2
- 12
- 33
2
votes
1 answer
How To Make Reverse Relationships One To Many In Laravel
I have two models that are User and Transactions and I have made a relationship OneToMany, my hasMany Relationship is working fine, but its reverse relationship is not working here is my Transaction Migration.
Schema::create('transactions',…

Adnan Ali
- 61
- 6
2
votes
1 answer
How to use Laravel's hasManyThrough on self-referenced tables
I have the User and Regional models, which I want to get some regionals from the user type of supervisor. So here is how I define the relationship.
public function regional()
{
return $this->belongsTo(Regional::class);
}
/**
* Get all user…

Matius Nugroho Aryanto
- 801
- 3
- 17
- 41
2
votes
2 answers
Laravel eloquent with() nested relationship fields in parent unnested
the elequent query i have is:
$rows = ProjectLogin::with(
'User:PRIVATE,naam',
'Project:PRIVATE,naam',
'Queue:PRIVATE,queue_naam',
'AgentStatus:PRIVATE,status'
)
->limit(10)->get();
this will return this array if i do…

fidderr
- 53
- 8
2
votes
1 answer
How can I check a variable exist in returned collection relationship or not
I have this relationship in User.php Class:
public function favourites()
{
return $this->belongsToMany(Product::class, 'favourite_products', 'usr_id', 'prd_id')->withTimestamps();
}
And I have added $user_favourites =…

yetep93258
- 33
- 1
- 13
2
votes
0 answers
What are the relationship for FavouriteProduct Model
I'm working on an Online Store project with Laravel 5.8 and in this project, I wanted to add "Add to favourites" ability for users to add a product to their favourite list.
So I created a Model like this:
class FavouriteProduct extends Model
{
…

yetep93258
- 33
- 1
- 13
2
votes
1 answer
laravel relationship with json column
//controller
$promotion = Promotion::findOrFail($id);
//return
Array
(
[id] => 2
[en_title] => promo1
[game_id] => Array
(
[0] => 3
…

ventures 999
- 149
- 1
- 3
- 12