How can I specify a relationship for my Text Filter?
I've set up my datatable query to use my Customer and Account models like so:
$table
-> query(
Customer::query()
->with('account')
)
);
I'm now trying to create some filters, one of which uses a column from my accounts db table. However the below approach doesn't work because the db table "accounts" is not actually available to use in the "where" clause.
Filter::make('account_name_filter')
->form(
[TextInput::make('account_name')->label('Account ID')])
->query(function (Builder $query, array $data): Builder {
return $query->when(
$data['account_id'],
fn(Builder $query, $value): Builder => $query->where('accounts.account_id', 'ILIKE', "%{$value}%")
);
})
I realise I can work around this by instead fetching data by joining on the tables specifically:
Customer::query()
->join('accounts as acc', 'customers.id', '=', 'account.customer_id')
I'm just wondering if there is an alternative approach that just uses the eloquent models instead, perhaps by defining the relationships between the models?
0 Replies