livewire-datatables icon indicating copy to clipboard operation
livewire-datatables copied to clipboard

livewire-datatables with raw query

Open sixtyrc opened this issue 2 years ago • 3 comments

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

sixtyrc avatar Jan 11 '22 19:01 sixtyrc

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

abbasmashaddy72 avatar Jan 17 '22 10:01 abbasmashaddy72

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 ?

Sicklou avatar Feb 04 '22 21:02 Sicklou

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

mmegue avatar Feb 27 '22 09:02 mmegue