-
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #454 from Power-Components/feature/searchable-raw
add searchableRaw
- Loading branch information
Showing
5 changed files
with
206 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace PowerComponents\LivewirePowerGrid\Tests; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Support\Facades\DB; | ||
use PowerComponents\LivewirePowerGrid\Tests\Models\Dish; | ||
use PowerComponents\LivewirePowerGrid\Traits\ActionButton; | ||
use PowerComponents\LivewirePowerGrid\{Column, | ||
Footer, | ||
Header, | ||
PowerGrid, | ||
PowerGridComponent, | ||
PowerGridEloquent}; | ||
|
||
class DishesSearchableRawTable extends PowerGridComponent | ||
{ | ||
use ActionButton; | ||
|
||
public string $database = ''; | ||
|
||
public function setUp(): array | ||
{ | ||
$this->showCheckBox(); | ||
|
||
return [ | ||
Header::make() | ||
->showSearchInput(), | ||
|
||
Footer::make() | ||
->showPerPage() | ||
->showRecordCount(), | ||
]; | ||
} | ||
|
||
public function datasource(): Builder | ||
{ | ||
$searchableRaw = match ($this->database) { | ||
'sqlite' => 'STRFTIME("%d/%m/%Y", dishes.produced_at) as produced_at_formatted', | ||
'mysql' => 'DATE_FORMAT(dishes.produced_at, "%d/%m/%Y") as produced_at_formatted', | ||
'pgsql' => 'to_char(dishes.produced_at, \'DD/MM/YYYY\')::text as produced_at_formatted', | ||
default => '' | ||
}; | ||
|
||
return Dish::query() | ||
->join('categories', function ($categories) { | ||
$categories->on('dishes.category_id', '=', 'categories.id'); | ||
}) | ||
->select('dishes.*', 'categories.name as category_name', DB::raw($searchableRaw)); | ||
} | ||
|
||
public function addColumns(): PowerGridEloquent | ||
{ | ||
return PowerGrid::eloquent() | ||
->addColumn('id') | ||
->addColumn('name') | ||
->addColumn('produced_at_formatted'); | ||
} | ||
|
||
public function columns(): array | ||
{ | ||
$searchableRaw = match ($this->database) { | ||
'sqlite' => 'STRFTIME("%d/%m/%Y", dishes.produced_at)', | ||
'mysql' => 'DATE_FORMAT(dishes.produced_at, "%d/%m/%Y")', | ||
'pgsql' => 'to_char(dishes.produced_at, \'DD/MM/YYYY\')::text', | ||
default => '' | ||
}; | ||
|
||
return [ | ||
Column::make('ID', 'id') | ||
->searchable() | ||
->sortable(), | ||
|
||
Column::make('Prato', 'name') | ||
->searchable() | ||
->editOnClick() | ||
->clickToCopy(true) | ||
->makeInputText('name') | ||
->placeholder('Prato placeholder') | ||
->sortable(), | ||
|
||
Column::make('Produced At Formatted', 'produced_at_formatted') | ||
->searchableRaw($searchableRaw) | ||
->sortable(), | ||
]; | ||
} | ||
|
||
public function bootstrap() | ||
{ | ||
config(['livewire-powergrid.theme' => 'bootstrap']); | ||
} | ||
|
||
public function tailwind() | ||
{ | ||
config(['livewire-powergrid.theme' => 'tailwind']); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
use function Pest\Livewire\livewire; | ||
|
||
it('searches data using whereRaw on sqlite', function (string $component, object $params) { | ||
livewire($component, ['database' => 'sqlite']) | ||
->call($params->theme) | ||
->set('search', '09/09/2021') | ||
->assertSee('Polpetone Filé Mignon') | ||
->assertDontSee('Barco-Sushi Simples') | ||
->assertDontSee('No records found') | ||
# 09/09/2022 | ||
->set('search', '09/09/2022') | ||
->assertSee('No records found') | ||
# 06/2026 | ||
->set('search', '06/2026') | ||
->assertSee('Francesinha') | ||
->assertDontSee('Polpetone Filé Mignon'); | ||
})->with('searchable-raw')->requiresSQLite(); | ||
|
||
it('searches data using whereRaw on mysql', function (string $component, object $params) { | ||
livewire($component, ['database' => 'mysql']) | ||
->call($params->theme) | ||
->call($params->theme) | ||
->set('search', '09/09/2021') | ||
->assertSee('Polpetone Filé Mignon') | ||
->assertDontSee('Barco-Sushi Simples') | ||
->assertDontSee('No records found') | ||
# 09/09/2022 | ||
->set('search', '09/09/2022') | ||
->assertSee('No records found') | ||
# 06/2026 | ||
->set('search', '06/2026') | ||
->assertSee('Francesinha') | ||
->assertDontSee('Polpetone Filé Mignon'); | ||
})->with('searchable-raw')->requiresMySQL(); | ||
|
||
it('searches data using whereRaw on pgsql', function (string $component, object $params) { | ||
livewire($component, ['database' => 'pgsql']) | ||
->call($params->theme) | ||
->set('search', '09/09/2021') | ||
->assertSee('Polpetone Filé Mignon') | ||
->assertDontSee('Barco-Sushi Simples') | ||
->assertDontSee('No records found') | ||
# 09/09/2022 | ||
->set('search', '09/09/2022') | ||
->assertSee('No records found') | ||
# 06/2026 | ||
->set('search', '06/2026') | ||
->assertSee('Francesinha') | ||
->assertDontSee('Polpetone Filé Mignon'); | ||
})->with('searchable-raw')->requiresPostgreSQL(); |
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