MedicOneSystems / livewire-datatables

Advanced datatables using Laravel, Livewire, Tailwind CSS and Alpine JS
https://livewire-datatables.com/
MIT License
1.2k stars 259 forks source link

livewire-datatables with raw query #380

Open sixtyrc opened 2 years ago

sixtyrc commented 2 years ago

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

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

abbasmashaddy72 commented 2 years ago

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: 1) https://github.com/MedicOneSystems/demo-livewire-datatables/blob/master/app/Http/Livewire/ComplexDemoTable.php 2) https://github.com/MedicOneSystems/livewire-datatables#custom-column-names

Sicklou commented 2 years ago

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 ?

mmegue commented 2 years ago

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()