livewire-datatables
livewire-datatables copied to clipboard
livewire-datatables with raw query
Hello, it is possible to use a raw query instead of a model to use the livewire datables, all the examples that I see always use for example <livewire: datatable model = "App \ User" exclude = "planet_id, bio, latitude, longitude, updated_at" />
instead of the model, I would like to use for example: $ pac = DB :: select ('EXEC IHByDate "'. $ month. '", "'. $ year. '"'); which is a stored procedure in my SQL, is it possible? I'm trying to use the type of datatables that are here: https://livewire-datatables.com/actions
Yes,
DateColumn::raw('dob AS dob2') ->label('Birthday') ->format('jS F') ->sortBy(DB::raw('DATE_FORMAT(users.dob, "%m%d%Y")')),
The above one is the example. for detailes you follow this below links:
- https://github.com/MedicOneSystems/demo-livewire-datatables/blob/master/app/Http/Livewire/ComplexDemoTable.php
- https://github.com/MedicOneSystems/livewire-datatables#custom-column-names
I got the same question.
Sometimes I don't have a model but a raw complex query.
Is there a way to provide maybe a collection instead of a model ?
I don't know if this is the correct way of doing this. this is how I managed to do it. You can return your complex query as a view[SQL Views].
*Create a migration to add a view php artisan make:migration create_my_complex_query
*In your migration file
public function up()
{
\DB::statement($this->createView());
}
public function down()
{
\DB::statement($this->dropView());
}
private function createView(): string
{
return <<<SQL
CREATE VIEW my_complex_query AS
//your complex query here
SQL;
}
private function dropView(): string
{
return <<<SQL
DROP VIEW IF EXISTS `my_complex_query`;
SQL;
}
*Run php artisan migrate
*Create a model MyComplexView
*Then add public $table = "my_complex_query";
*Then use the model MyComplexView::query()