You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to filter a table with a relationship by the relation's attribute.
example: Let's say I have a table with all my posts, and a column is displaying the author name. But when I entered something in the author filter, I can only use the author's id. It returns no result when I enter an author's name.
In list screen public function query(): array { return [ 'posts' => Post::filters()->with('author')->defaultSort('id', 'desc')->paginate() ]; }
in list layout (not sure if that's the best way to do that BTW) TD::make('author', 'Author') ->filter(TD::FILTER_TEXT) ->render(function ($post) { return Author::where('id', $post->author)->first()->name; }),
So my table show my author's name but I can only use his ID to filter, not his name :/
Hi, you can do it using Eloquent filters, which are described here https://orchid.software/en/docs/filters/#eloquent-filter
The default distribution already has an example of looking for a user by role. Note that you can combine HTTP and Eloquent filters to achieve the desired graphical display.
I had the same question as you and I managed to get it to work.
What I did was I created a filter using this command php artisan orchid:filter JournalFilter which is mentions here https://orchid.software/en/docs/filters/#eloquent-filter
And then on my screen where I get the table data I did: Journal::filters()->filtersApply([JournalFilter::class])->defaultSort('id', 'desc')->paginate()
Here is the content of my files: JournalFilter.php
`<?php
namespace App\Orchid\Filters;
use App\Models\Journal;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Orchid\Filters\Filter;
use Orchid\Screen\Field;
use Orchid\Screen\Fields\Select;
class JournalFilter extends Filter
{
/**
* @var array
*/
public $parameters = ['filter.createdBy'];
/**
* @return string
*/
public function name(): string
{
return '';
}
/**
* @param Builder $builder
*
* @return Builder
*/
public function run(Builder $builder): Builder
{
$request = $this->request;
return $builder->whereHas('createdBy', function (Builder $query) use ($request) {
$query->where(DB::raw('CONCAT(" ", firstname, lastname)'), 'LIKE', '%'.$request->input('filter.createdBy').'%');
});
}
/**
* @return Field[]
*/
public function display(): array
{
return [];
}
}
`
Journal.php (Journal model)
`
hasOne(User::class, 'id', 'created_by')->withoutGlobalScope(UserScope::class);
}
}
`
and here is the table column layout which I have applied such this filter on:
`TD::make('createdBy', __('Created By'))
->sort()
->cantHide()
->filter(TD::FILTER_TEXT)
->render(function (Journal $journal) {
return new Persona($journal->createdBy->presenter());
}),`
I hope this helps and I believe this is exactly what you were looking for :)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I'd like to filter a table with a relationship by the relation's attribute.
example: Let's say I have a table with all my posts, and a column is displaying the author name. But when I entered something in the author filter, I can only use the author's id. It returns no result when I enter an author's name.
In list screen
public function query(): array { return [ 'posts' => Post::filters()->with('author')->defaultSort('id', 'desc')->paginate() ]; }in list layout (not sure if that's the best way to do that BTW)
TD::make('author', 'Author') ->filter(TD::FILTER_TEXT) ->render(function ($post) { return Author::where('id', $post->author)->first()->name; }),So my table show my author's name but I can only use his ID to filter, not his name :/
Thanks
All reactions