Skip to content

Instantly share code, notes, and snippets.

@G3z
Last active November 2, 2021 19:32
Show Gist options
  • Save G3z/92868f75a56db984dbdf10ae6dc768f0 to your computer and use it in GitHub Desktop.
Save G3z/92868f75a56db984dbdf10ae6dc768f0 to your computer and use it in GitHub Desktop.
filtering related
class AccountContactUser extends Model
{
use HasFactory;
use Filterable;
/**
* @var array
*/
protected $allowedFilters = [
'sap_id',
'mobile',
'email',
'phone',
'function_description',
'valid_to',
];
/**
* @var array
*/
protected $allowedSorts = [
'sap_id',
'mobile',
'email',
'phone',
'function_description',
'valid_to',
'contact.surname',
];
public function contact()
{
return $this->belongsTo(Contact::class);
}
}
TD::make('contacts.contact.name', __('Name'))
->sort()
->render(function (AccountContactUser $relation) {
return $relation->contact->name ?? '';
}),
TD::make('contacts.contact.surname', __('Surname'))
->sort()
->render(function (AccountContactUser $relation) {
return $relation->contact->surname ?? '';
}),
'contacts' => AccountContactUser::with(['account'])
->filtersApplySelection(ContactSelection::class)
->defaultSort('id', 'desc')
->paginate(),
<?php
namespace App\Orchid\Filters\Contact;
use Illuminate\Database\Eloquent\Builder;
use Orchid\Filters\Filter;
use Orchid\Screen\Field;
use Orchid\Screen\Fields\Input;
class ContactSurnameFilter extends Filter
{
/**
* @var array
*/
public $parameters = ['surname'];
public function name(): string
{
return __('Surname');
}
public function run(Builder $builder): Builder
{
return $builder->when($this->request->has('surname'), function (Builder $query) {
$query->whereHas('contact', function (Builder $query) {
$query->where('surname', 'LIKE', '%'.$this->request->get('surname').'%');
});
});
}
/**
* @return Field[]
*/
public function display(): array
{
return [
Input::make('surname')
->type('text')
->value($this->request->get('surname'))
->placeholder('Search...')
->title(__('Surname')),
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment