giorg-332048's avatar

adminLTE and datasource() filter

Hi,

I'm using Laravel 12 with sebastian heyd boiler plate (first day I post here so I cannot link the referring site. I'm trying to setup a datasource for a datatable, which works fine:

class SubscriptionsDatatable extends Datatable { public $slug = 'subscriptions';

public function datasource()
{
    return Subscription::with('customer');
}

...

and in the blade I use:

<x-boilerplate::datatable name="subscriptions" id="subscriptions" />

all good this far, but as I try to search for example customer name it fails, I understood must something about query server side. I tried many solutions using claude or perplexity, but I'm not able to filter by customer name (which is obviously a join). How to I achieve that?

thanks a lot Andrea

0 likes
3 replies
Nakov's avatar

You can find that in the documentation of the boilerplate that you are using: https://sebastienheyd.github.io/boilerplate/docs/8.x/datatables/column.html#filter

So basically when you are adding the column you can add a custom filter for it:

use Illuminate\Database\Eloquent\Builder;

Column::add('Customer')
        ->data('customer.name')
		->name('customer.name'), // based on https://sebastienheyd.github.io/boilerplate/docs/8.x/datatables/column.html#name

       // or https://sebastienheyd.github.io/boilerplate/docs/8.x/datatables/column.html#filter

->filter(function($query, $q) {
    $query->whereHas('customer', function(Builder $query) use ($q) {
        $query->where('name', '=', $q);
    });
}) 
giorg-332048's avatar

ok sorry, I was too quick being excited. This is my SuspensionDatatable:

because in the Model a Subscription can have multiple Suspension and belongs to one Customer, so my Suspension model has a method:

public function subscription(): BelongsTo { return $this->belongsTo(Subscription::class); }

so I thought: suspension->subscription->customer but I get Datatable error. I also tried adding 'customer' in the with statement, but same result. last try I did:

Column::add(__('Last Name'))
                ->data('subscription.id', function($s) {
                    $customer = Customer::findOrFail($s->subscription->customer_id);
                    return $customer->last_name;
                }),

which shows me table but then I don't know how to add also first name and email which both belong to customer model. this way, also the search is not working. how to fix everything? many thanks

Please or to participate in this conversation.