laravel models accessing results from hasManyThrough with 3 tables - laravel

I have three tables:
PhaseTasks
phaseId
taskId
1
1
1
2
2
4
Tasks
taskId
taskName
defaultResource
1
Do a thing
1
2
Do another thing
1
3
do some other thing
2
resources
id
name
1
Engineering
2
Support
2
Sales
I am trying to get a result set where i can print
Phase Id: 1, TaskName: Do a thing, ResourceName: Engineering
Phase Id: 1, TaskName: Do another thing, ResourceName: Engineering
Here is my phaseTasks Model:
public function phaseTasks(){
return $this->hasManyThrough(
resources::class,
tasks::class,
'id',
'id',
'taskId',
'defaultResource'
);
}
and my controller:
$phaseData = phaseTasks::with('phaseTasks')->where('phaseId','1')->get();
i cant get the task table info, nor can i figure out how to access the resource data. I DO see the resource data in the print_r($phaseData), i do not see the tasks stuff, im guessing i need to get into pivots for that part.
***EDIT
I was able to figure out how to get the resources.name entity
$phaseData[0]->phaseTasks[0]->name;
now i just need to figure out how to get the taskName entity.

I'm not sure but I think you got the relationship the other way around: it's not hasmany (through) but belongs to.
So, phasetasks - task_id belongs to a task - default resource belongs to a resource
And your phasetasks looks like a pivot table in itself, but that may be ok.
So if you agree with my assumption and create the relations my way, try something like
PhaseTask::with('task.resource')->get();

Related

How to remove and update data on relations table using laravel eloquent?

I have three tables. they are,
users
id name
1 personA
2 personB
3 personC
skills
id name
1 html
2 css
3 javascript
userskills
userid skills_id
2 3
2 1
1 2
1 3
I get the data through the request like [2,1] and the user id 2. On the user skills table in userid 2 there are skills_id3,1. Now i have to remove skills_id 3 and insert skills_id 2 on the userid 2.
How can i achieve it through laravel eloquent method.
As you have a pivot table you can use belongsToMany relationship. Adding the relationship in your User Model
public function skills()
{
return $this->belongsToMany(Skill::class, 'userskills','userid', 'skills_id');
//userskills is the pivot table
}
Now to update use the sync method.
$user = User::findOrFail(2); //your request id
$user->skills()->sync([1, 2]); //request array
This will remove the 3 from the pivot table and add 2 in the table.
Read more about this relationship in here
Your question is little ambiguous but lets assume that you are getting the user id 2 and the skills you want to remove from userskills is 2,1
For deleting
UserSkills::where(['userid' => 2 ])->whereIn('skills_id',[2,1])->delete();
For inserting
UserSkills::create(['skills_id' => 2, 'userid' => 2 ]);
An easier approach using relation would be
$user->skills()->sync([2, 1]);
Sync will remove those skills which are not present in the sync method.

How do I show a table's Has Many relationship on frontend?

Using Laravel + Voyager, I have a "Has Many" relationship:
Course hasMany Teachers.
In backend all is fine, but if I try to get the information in the frontend, I only get the value in table, not the relation, so the output goes something like:
0
id 1
teachers_id null
name "Math"
1
id 2
teachers_id null
name "English"
Current code in web routing:
Route::get('/course', function () {
$co= App\Course::all();
return $co;
});
How can I get the correct output?
teachers_id Xav, Titus
so like #Tpojka commented you can start by eager loading the relationship when you are initializing the course likeso $co = App\Course::with('teachers')->get(); after which you do not need to make another unnecessary call to your database for the teachers of that course. You can get a collection of all the teachers of that course by simply calling $teachers = $co->teachers; here $teachers is now a laravel collection you can simply loop through it on the client side and display the information about the teachers you want to display. I hope this helps.
Good luck and happy coding. :)

Laravel belongsToMany pivot with multiple columns

I currently have two tables in the DB and a pivot table to join them when I need to do a belongsToMany lookup. The basic example is one DB table is 'teams' and the other is 'members'. I can utilize the belongsToMany method on both the team and members model to pull their relationship with each other. A team can have many members, and a member can belong to many teams.
public function teams()
{
return $this->belongsToMany(Team::class);
}
public function members()
{
return $this->belongsToMany(Member::class);
}
Pivot: team_member
team_id | member_id
---------------------
1 | 1
2 | 1
3 | 2
1 | 2
How can I expand on that pivot table to include a type of member for each team? For example, member1 is a leader on team1. member1 is an assistant on team2. member1 is a generic member on team3... and so on. Can I just add a column to that same pivot table? Would it be the membertype_id? How can I relate that to another model/table?
This is pretty common, and Laravel handles it already. Add extra columns to the pivot table and expand your relationships to use withPivot():
public function teams(){
return $this->belongsToMany(Team::class)->withPivot(["teamRole", ...]);
}
Then accessing is as simple as:
$user = \App\User::with(["teams"])->first();
$team = $user->teams->first();
$teamRole = $team->pivot->teamRole;
See the Documentation for more information:
https://laravel.com/docs/5.6/eloquent-relationships
To answer the second part, which is essentially a "Triple Pivot", that requires extra functionality. See
Laravel - Pivot table for three models - how to insert related models?
for more information, and an associated Package (by the answerer on that question, not maintained, but good for an example)
https://github.com/jarektkaczyk/Eloquent-triple-pivot

group method for activerecord. Docs are confusing?

I don't get what this means in the Rails tutorial:
group(*args) public
Allows to specify a group attribute:
User.group(:name)
=> SELECT "users".* FROM "users" GROUP BY name
Returns an array with distinct records based on the group attribute:
User.group(:name) [User id: 3, name: "Foo", ...>, #User id: 2, name: "Oscar", ...>]
I don't see the grouping with the example they gave...
Group is most useful (I think) if you are trying to count stuff in your database or if you join multiple tables. Let me give a few examples.
1.
If you want to know how many users there are in your data base with each name then you can do:
User.group(:name).count
this will return a hash looking something like this:
{ ann: 4, bert: 15, cecilia: 3 ... }
I do not know why there are so many Berts in your database but anyway...
2.
If your users have related records (for instance cars) the you can use this to get the first car included in your activerecord model (the reason it will be the first is because of how group works and is further explained in the link below)
User.joins(:cars).select('users.*, cars.model as car_model, cars.name as car_name').group('users.id')
Now all records in this result will have a method called car_model and one called car_name.
You can count how many cars each user has with one single query.
User.joins(:cars).select('users.*, count(cars.id) as car_count').group('users.id')
Now all records will have a car_count.
For further reading: Mysql group tutorial
Hope this shed enough light over groups for you to try them out a little bit. I do not think you can fully understand them until you worked with them a little bit.

Laravel: get Many-to-Many column data

Yo all,
I have a users relationship pivot db table as follows:
id | user_id | relation_id | relationship
1 4 2 tutor
1 4 3 parent
The table relates user with one-and-other for various reasons.
I am trying to get the relationship column within the $user. I have managed to pull the related users
details no problem - $user->relations.
However, I just need to get the relationship - eg. Tutor or parent.
I am getting no dice with $relative->pivot->relationship
Any ideas? Thanks for taking the time to help.
#foreach($user->relations as $index=>$relative)
{{ $relative->first_name . ' ' . $relative->last_name}}
{{ $relative->pivot->relationship }}
#endforeach
To access ->pivot->whatever you need to add withPivot('whatever') to the relation definition:
public function relations()
{
return $this->belongsToMany('Relation')->withPivot('relationship');
}
Note: I wouldn't use relations and Relation names here, since it's misleading AND it may collide with Eloquent\Model stuff.

Resources