Last active
November 2, 2021 19:32
-
-
Save G3z/92868f75a56db984dbdf10ae6dc768f0 to your computer and use it in GitHub Desktop.
filtering related
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ?? ''; | |
}), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'contacts' => AccountContactUser::with(['account']) | |
->filtersApplySelection(ContactSelection::class) | |
->defaultSort('id', 'desc') | |
->paginate(), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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