Many-to-Many relationship - get name from third table
Hi everyone, I'm new in Laravel/Filament 👀 So maybe my question is very trivial.
So I have subjects and courses tables, which have many-to-many relationship (there is a course_subject pivot table). In course table there is an another foreign key - department_id.
I want to on Subject resource page on Select and Column Field get course with Department name from departments table. On second pic 1, 2 are courses: But I want to show like Department name + Course name.
Thanks
25 Replies
I think I found solution:) I just needed to call courses.department.name
courses.department.name only get Department name
I need also concatenate Course name too, and also need to get Department name + Course name in Select
how does your model looks? Subject (model)?
Subject Model
public function chair()
{
return $this->belongsTo(Chair::class);
}
public function courses()
{
return $this->belongsToMany(Course::class);
}
Course Model
public function department()
{
return $this->belongsTo(Department::class);
}
public function subjects()
{
return $this->belongsToMany(Subject::class);
}
Trying with this
Tables\Columns\TextColumn::make('courses.department.name')
->label('Courses')
->formatStateUsing(function ($state, Subject $subject) {
return $subject->courses;
})
->badge()
->sortable(),
but getting only last jsonTry
->formatStateUsing(fn (Model $record) => "{$record->courses->name} - {$record->courses->department->name}")
Tried, but problem is that I get same json array like in picture mentioned above
also I need to call [0] like this
->formatStateUsing(fn (Model $record) => "{$record->courses[0]->course} - {$record->courses[0]->department->name}")
Maybe do a foreach inside and return each course? (dont know if it works like this)
->formatStateUsing(function (Model $record) {
foreach($record->courses as $course) {
return "{$course->name} - {$course->department->name}");
}
}
No it not help, same thing
In one word I want to concatenate two strings like in native PHP
'courses.department.name'.' '.'courses.course'.' term'
Since your relationship is
belongsToMany
yes you'll get array of data then you have to do [0]
You can check this (https://laravel.com/docs/11.x/eloquent-relationships#has-one-of-many) to return latest/oldest of many and you don't need to check [0] anymore.Ok, but why I get same array?
when using formatStateUsing
Not sure, can you run this below code and check the output:
Let's see how many courses your subject had, maybe this explains
I checked, it get all courses, but when using formatStateUsing to concat, then it gets same course, see pictures above
Tables\Columns\TextColumn::make('courses.department.name')
->label('Courses')
->formatStateUsing(fn (Model $record) =>
"{$record->courses[0]->course} - {$record->courses[0]->department->name}")
->badge()
->sortable(),
with thisTables\Columns\TextColumn::make('courses.department.name')
->label('Courses')
// ->formatStateUsing(fn (Model $record) =>
// "{$record->courses[0]->course} - {$record->courses[0]->department->name}")
->badge()
->sortable(),
with thisShould be General Medicine 1, General Medicine 2, Stomatology 1
Does your department name contain
General Medicine 1
etc because courses.department.name
will display the department name for all your coursessee this
department name is General Medicine
Course is 1 which belongsTo Department General Medicine, etc...
Without using
->formatState....
it's displaying correct, try changing courses.name
to see the difference because if you use courses.department.name
then it'll display only department name for all courses
This one is form, but now we're talking about table 🧐courses.name will return course name only - 1
but course table have foreign department_id
I want to get Department name from departments table and course name from courses table
Deapartment + Course
Use
courses.name
and try like below:
no luck
dd($record) inside format state, what did you get?
Solved with this try
Now will try same thing with Select Form
Please note I named Course name as course
With Select