John Davidson

php - Put the value of listener to a query builder (using Laravel + livewire + datatables)

0 comments
Message:


This is my firstime a ask for something in StackOverflow.
I'm using datatables from https://github.com/MedicOneSystems/livewire-datatables with a Laravel and livewire project.
I wanna put the return value from a listener to a query builder to filter the result by dates (Date From and Date To)


My listener from my livewire coponents (DateFilter.php) :


$this->emit('dateFromChange', $this->dateFrom);
$this->emit ('dateToChange', $this->dateTo);

How i get it from my datatable component (MetiersDataTable.php) regardless of the documentation of Livewire :


protected $listeners = ['dateFromChange','dateToChange'];

public function dateFromChange($date){
$dateFrom = $date;
return $dateFrom;
}

public function dateToChange($date){
$dateTo = $date;
return $dateTo;
}

This is my function (MetiersDataTable.php) where my query is returned but the function doesn't support arguments ...


public function builder()
{
return Metier::query()
->leftJoin("leads", "leads.metier_id", "metiers.id")
->whereNotIn('leads.etape_id',[6])
->groupBy('metiers.nom');
}

Does anyone have a solution?



My Answer:
listener('dateSelected', function ($dateFrom, $dateTo) {
$this->dateFrom = $dateFrom;
$this->dateTo = $dateTo;
});_In my livewire-datatables view (datatable.blade.php), I have the following code:__In my datatable component (Datatable.php), I have the following code:_public function query()
{
return Model::query()
->whereBetween('created_at', [$this->dateFrom, $this->dateTo]);
}_With this code, the query builder will filter the results based on the date range selected by the user.

Rate this post

5 of 5 based on 1488 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us