Laravel hasManyThrough returning empty array - laravel

I need some help regarding hasManyThrough()
user_duty
id
user_id
date
duty_hours
sales_duty
id
user_id
date
total_sales
total_orders
income_table
id
sales_id (this is same as 'id' of sales_duty)
date (same as 'date' of sales_duty)
total_income
total_collection
I can relate sales_duty with income_table using (sales_id,date) these two keys.
I can also connect to sales_duty from user_duty based on (user_id,date)
Now I want to connect to user_duty to income_table
public function user_income()
{
return $this->hasManyThrough('App\IncomeTable','App\SalesTable','id','sales_id','id','id');
}
But its returning empty

According to the details you have provided it should be like this.
public function user_income()
{
return $this->hasManyThrough(
'App\IncomeTable',
'App\SalesTable',
'user_id', // Foreign key on sales_duty table...
'sales_id', // Foreign key on income_table...
'id', // Local key on user_duty table...
'id' // Local key on sales_duty table...
);
}

Related

Laravel get column name using child table with eager loading

I want to fetch the data from profile table using a child table of other parent table.
Here is the tables
Profile table
id
profile_name
Transaction table
id
transaction_name
Sub transaction table
id
transaction_id
profile_id
Now i want to get the profile name from transaction model using eager loading . i tried has one Though relationship in transaction model but returning null
Here is my relationship in Transaction model
public function profileName()
{
return $this->hasOneThrough(
Profiler::class,
SubTransaction::class,
'profile_id',
'id',
'id',
'transaction_id',
);
}
Here is where i am trying to fetch from transaction controller
$options = Transaction::with([
'profileName:id,profile_name as profileName',
])
->get();
It returns null because I think you have a little problem about matching between foreign keys and local keys. You could try the following code:
return $this->hasOneThrough(
Profiler::class, //Final model we wish to access
SubTransaction::class, //The name of the intermediate model
'transaction_id', //Foreign key on sub_transaction table
'id', //Foreign key on profile table
'id', //Local key on transaction table
'profile_id', //Local key on sub_transaction table
);
If you have any problem, tell me.

best way to retrive a collection from -many to many- connected to -one to many- relation (laravel7)

you can see a part of my database in the image below:
the goal of this design was to able the admins send alerts to users filtered by center and field(dynamically). if you think this is a bad design tell me (please say why and how should I improve my design).
now if I want to get the alerts for a user I should do this:
$userAlerts = collect();
auth()->user()->branch()->first()->groups()->get()->each(function ($item, $key) use ($userAlerts) {
$item->alerts()->get()->each(function ($item, $key) use ($userAlerts) {
$userAlerts->push($item);
});
});
this code is ridiculous and I don't want to do it in this way.
instead i want to do something like this:
$alerts = auth()->user()->branch()->groups()->alerts() // i want this // method 1
or
$alerts = auth()->user()->alerts() //or maybe this // method 2
can I do something like this without changing the database? (method 1 or 2).
You can use laravel's default Has Many Through on branch model to pick associated alerts
class Branch extends Model
{
public function alerts()
{
return $this->hasManyThrough(
'App\Models\Alert',
'App\Models\Group',
'branch_id', // Foreign key on alert table...
'group_id', // Foreign key on group table...
'id', // Local key on branch table...
'id' // Local key on group table...
);
}
}
Uisng this way you can query user alerts as
$alerts = auth()->user()->branch()->alerts()
Or you can use third party package eloquent-has-many-deep which provides an ease to extend hasManyThrough on multiple related models
class User extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function alerts()
{
return $this->hasManyDeep(
'App\Models\Alert',
['App\Models\Branch', 'App\Models\Group'], // Intermediate models, beginning at the far parent (User).
[
'user_id', // Foreign key on the "branch" table.
'branch_id', // Foreign key on the "group" table.
'group_id' // Foreign key on the "alert" table.
],
[
'id', // Local key on the "user" table.
'id', // Local key on the "branch" table.
'id' // Local key on the "group" table.
]
);
}
}
And you can directly get user alerts as
$alerts = auth()->user()->alerts()
Lastly you can define alerts relation in user model by adding some joins to directly pull related alerts
class User extends Model
{
public function alerts()
{
return $this->belongsTO('App\Branch', 'branch_id')
->join('branch_group', 'branches.id', '=', 'branch_group.branch_id')
->join('alerts', 'branch_group.group_id', '=', 'alerts.group_id')
->select('alerts.*');
}
}

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 https://laravel.com/docs/5.6/eloquent-relationships#has-many-through from laravel documentation
here is my code from Result model
public function questions(){
return $this->belongsToMany('App\questions', 'results_questions', 'result_id', 'questions_id');
}
the code from question model
public function subject(){
return $this->belongsTo('App\Subject','subject_id');
}
and this is what i tried from documentation (doesnot work)
public function subjects(){
return $this->hasManyThrough(
'App\Subject',
'App\questions',
'subject_id', // Foreign key on question table...
'questions_id', // Foreign key on result table...
'id', // Local key on question table...
'id' // Local key on result table...
);
}**strong text**
Try to remove 5th and 6th parameters from your relation, since they are optional. Also, should your Questions model be lowercase?
public function subjects(){
return $this->hasManyThrough(
'App\Subject',
'App\questions', //*Should this be lowercase?*
'subject_id', // Foreign key on question table...
'questions_id', // Foreign key on result table...
);
}

yii 1.1.x ar relation for composite forign key

I have three tables
Boooking
id, -> primary key
name
Ticket
booking_id, (primary key)
seq , (primary key)
name
TicketAllocation
id, (primary key)
booking_id, (foreign key)
seq, (foreign key)
name,
date
Ticket table has primary key as composite of(booking_id,seq), and foreign key booking_id from Booking table.
and it works as foreign key in TicketAllocation table.
How can i define relation in both Ticket and TicketAllocation table so i can use AR relation to get related data.
Hii it you can simply use this
Ticket Model
public function relations()
{
return array(
'ticketToBoooking' => array(self::BELONGS_TO, 'Boooking', 'booking_id'),
);
}
TicketAllocation Model
public function relations()
{
return array(
'ticketAllcationToBooking' => array(self::BELONGS_TO, 'Boooking', 'booking_id'),
);
}

Laravel - Many to Many Polymorphic Relation with Extra Fields

How can I define a many to many polymorphic relation with extra fields?
I have three (or more, as it is a polymorphic relation) tables.
tags table: id, name
tagged table: id, tag_id, taggable_id, taggable_type, user_id
posts table: id, record, timestamps
users table: id, name, email
The user_id on the tagged table referes to the users table on column id.
In my Post model I have:
public function tags()
{
return $this->morphToMany('App\Tag', 'taggable','tagged');
}
and in my Tag model I have:
public function posts()
{
return $this->morphedByMany('App\Post', 'taggable','tagged');
}
Then when I am try this in my controller:
$tag = new \App\Tag(
array(
'tag'=>"someTag"
));
$tag->save()
$post = \App\Post::find($id);
$post->tags()->save($tag);
I get Integrity Constraint Violation for not having a user_id:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (hb.tagged, CONSTRAINT tagged_user_id_foreign FOREIGN KEY (user_id) REFERENCES users (id)) (SQL: insert into tagged (tag_id, taggable_id, taggable_type) values (26, 2, App\Resource)).
Which is somewhat expected, as I have never had the chance to define or declare the user_id field.
Also, I have tried withPivot() on tags relation as follows, to no avail:
public function tags()
{
return $this->morphToMany('App\Tag', 'taggable','tagged')->withPivot('user_id');
}
Like in the comment: withPivot has nothing to do with saving/creating. If you want to pass additional pivot data when saving, do this:
$post->tags()->save($tag, ['user_id' => $userId]);

Resources