Pass filter data to Sushi model

Sorry team, I feel like a dumb dumb and am sure this is dead easy. I just want to pass the filters to my Model that uses Sushi. There are a few hints at answers around but it is just not clicking in my head. As you can see below, I just need access to the filters so I can do stuff. Here is a super simplified version of my model:
namespace App\Models;

use App\Http\Controllers\AnimalController;
use Illuminate\Database\Eloquent\Model;
use Sushi\Sushi;

class AnimalModel extends Model
{
use Sushi;

public function getRows()
{
return [
[
'id' => 1,
'name' => 'Frog',
'count' => AnimalController::calculateMe($filter)// data based on filter
],
[
'id' => 2,
'name' => 'bird',
'count' => AnimalController::calculateMe($filter)// data based on filter
],

];
}
}
namespace App\Models;

use App\Http\Controllers\AnimalController;
use Illuminate\Database\Eloquent\Model;
use Sushi\Sushi;

class AnimalModel extends Model
{
use Sushi;

public function getRows()
{
return [
[
'id' => 1,
'name' => 'Frog',
'count' => AnimalController::calculateMe($filter)// data based on filter
],
[
'id' => 2,
'name' => 'bird',
'count' => AnimalController::calculateMe($filter)// data based on filter
],

];
}
}
Any advice you can give to this poor old brain would be appreciated.
5 Replies
403gtfo
403gtfoOP2w ago
YES!!! I think I got it working. More testing required. I found the filters in request() and it works!!!!!!
$year = (isset(request()->query()['tableFilters'])) ?
request()->query()['tableFilters']['filters']['year'] :
request()->input('components')[0]['updates']['tableFilters.filters.year'];
$year = (isset(request()->query()['tableFilters'])) ?
request()->query()['tableFilters']['filters']['year'] :
request()->input('components')[0]['updates']['tableFilters.filters.year'];
The true condition works on initial page load (as I have a default value in the filter. the false condition is where the updated state is. I need to add another filter to test if the [0] index goes walk about and make sure it doesnt lose it if another filter is changed... which I feel it might. "which I feel it might." it does... ok I can still work around that with sessions.
if (isset(request()->query()['tableFilters'])) {
$year = request()->query()['tableFilters']['filters']['year'];
Session::put('equis_table2_year', $year);
} else if (isset(request()->input('components')[0]['updates']['tableFilters.filters.year'])) {
$year = request()->input('components')[0]['updates']['tableFilters.filters.year'];
Session::put('equis_table2_year', $year);
} else {
$year = Session::get('equis_table2_year');
}
if (isset(request()->query()['tableFilters'])) {
$year = request()->query()['tableFilters']['filters']['year'];
Session::put('equis_table2_year', $year);
} else if (isset(request()->input('components')[0]['updates']['tableFilters.filters.year'])) {
$year = request()->input('components')[0]['updates']['tableFilters.filters.year'];
Session::put('equis_table2_year', $year);
} else {
$year = Session::get('equis_table2_year');
}
Clunky I know... but I only have 2 filters to care about so works for me and beats the lifecycle BS that was stopping me. Now I just have to deal with when the user clicks reset... I should be able to fire an action on that I think.
awcodes
awcodes2w ago
You shouldn’t have to pass anything to sushi. It respects the eloquent query.
403gtfo
403gtfoOP2w ago
I'm kind of bastardizing Sushi and making my own model data on the fly (with blackjack and hookers) 😛 Basically by forming an array in the sushi model some fields of which are calculations based on the filters like x number of y in year n. This allows me to use the searchable and sortable methods in the filament table which normally break when you use formateStateUsing(). So basically dynamic static sushi data fun time muahahahahahhaha It works now and I am super happy about it. I just had to check for the empty start to address the reset button.
if (isset(request()->query()['tableFilters'])) {
$year = request()->query()['tableFilters']['filters']['year'];
Session::put('equis_table2_year', $year);
} else if (isset(request()->input('components')[0]['updates']['tableFilters.filters.year'])) {
$year = request()->input('components')[0]['updates']['tableFilters.filters.year'];
Session::put('equis_table2_year', $year);
} elseif (empty(request()->input('components')[0]['updates'])) {
$year = date('Y') - 1;
}
if (isset(request()->query()['tableFilters'])) {
$year = request()->query()['tableFilters']['filters']['year'];
Session::put('equis_table2_year', $year);
} else if (isset(request()->input('components')[0]['updates']['tableFilters.filters.year'])) {
$year = request()->input('components')[0]['updates']['tableFilters.filters.year'];
Session::put('equis_table2_year', $year);
} elseif (empty(request()->input('components')[0]['updates'])) {
$year = date('Y') - 1;
}
awcodes
awcodes2w ago
You’re definitely bastardizing sushi. 😂 But you shouldn’t have to is just an eloquent wrapper, so run the query you need and process the data after the query is run.

Did you find this page helpful?