PHP code example of joydeep-bhowmik / livewire-datatable

1. Go to this page and download the library: Download joydeep-bhowmik/livewire-datatable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

joydeep-bhowmik / livewire-datatable example snippets


 // datatable service provider
JoydeepBhowmik\LivewireDatatable\Providers\DataTableServiceProvider::class,





namespace App\Livewire;

use App\Models\User;
use Illuminate\Support\Carbon;
use JoydeepBhowmik\LivewireDatatable\Datatable;

class Table extends Datatable
{
    public $model = User::class;
    public function table()
    {
        //table method must return an array
        return [
            // id field
            $this->field('id')
                ->label('Id')
                ->sortable(),
                /* you can also run custom query for sort
                ->sortable(function($query,$direction){
                    $query->orderBy('id', $direction);
                })
                */
            // email field
            $this->field('email')
                ->label('Email')
                ->searchable(),
               /* you can also run custom query for search like
                ->searchable(function($query,$keyword){
                    $query->where('email', 'like', '%' . $keyword . '%')
                })
                */
            // created at
            $this->field('created_at')
                ->label('Created At')
                ->value(function ($row) {
                    return Carbon::createFromTimeStamp(strtotime($row->created_at))->diffForHumans();
                })
                /*
                You can also give it a text value like
                ->value('ok')
                */
                ->sortable(),
        ];
    }
}
 
     public function table()
    {
        return [
            $this->field('id')
                ->label('Id')
                ->table('products')
                ->as('product_id')// this is optional add according to your query
                ->sortable()
                ->searchable()
            ,
            $this->field('id')
                ->label('Stock Id')
                ->table('stocks')
                ->as('stock_id')// this is optional add according to your query
                ->sortable()
                ->searchable()
                //add more here
        ];
    }           

// Example: Define filters
public function filters()
{
   return [
            //input type select
            $this->filter('visibility')
                ->label('Visibility')
                //this options are        ->query(function ($query, $value) {
                    $query->where('products.id',$value);
                }),
            $this->filter('stock_id')
                ->label('Stock id')
                ->type('text')
                ->placeholder('Enter text id')
                ->query(function ($query, $value) {
                    $query->where('products.id', $value);
                })
                /*
                ->value('some text') //optional
                */,
            $this->filter('stock')
                ->label('In stock')
                ->type('checkbox')
                ->query(function ($query, $value) {
                    $query->where('products.id', $value);
                }),
                //add other filters
        ];
}

public $checkbox = true;
public $primaryKey = "id";
public function delete(){
    foreach($this->ids as $id){
        $product=Product::find($id);
        $product->delete();
    }
}
public function bulkActions()
    {
        return [
            $this->button('delete')
                ->text('Delete')
                ->action('delete')//this is a public method of your component
                ->confirm('Are u sure?')// optional,
        ];
    }

 
class Table extends Datatable
{
public $headers=false;

}
cmd
php artisan vendor:publish --tag=joydeep-bhowmik-livewire-datatable-css