jQuery DataTables API for Laravel 4|5|6|7|8

Overview

jQuery DataTables API for Laravel 4|5|6|7|8

Join the chat at https://gitter.im/yajra/laravel-datatables Donate Donate

Laravel 4.2|5.x|6|7|8 Latest Stable Version Latest Unstable Version Build Status Scrutinizer Code Quality Total Downloads License

This package is created to handle server-side works of DataTables jQuery Plugin via AJAX option by using Eloquent ORM, Fluent Query Builder or Collection.

return datatables()->of(User::query())->toJson();
return datatables()->of(DB::table('users'))->toJson();
return datatables()->of(User::all())->toJson();

return datatables()->eloquent(User::query())->toJson();
return datatables()->query(DB::table('users'))->toJson();
return datatables()->collection(User::all())->toJson();

return datatables(User::query())->toJson();
return datatables(DB::table('users'))->toJson();
return datatables(User::all())->toJson();

Requirements

Documentations

NOTE: Documentation links below are currently offline.

Laravel Version Compatibility

Laravel Package
4.2.x 3.x
5.0.x 6.x
5.1.x 6.x
5.2.x 6.x
5.3.x 6.x
5.4.x 7.x, 8.x
5.5.x 8.x
5.6.x 8.x
5.7.x 8.x
5.8.x 9.x
6.x.x 9.x
7.x.x 9.x
8.x.x 9.x

DataTables 8.x Upgrade Guide

There are breaking changes since DataTables v8.x. If you are upgrading from v7.x to v8.x, please see upgrade guide.

Quick Installation

$ composer require yajra/laravel-datatables-oracle:"~9.0"

Service Provider & Facade (Optional on Laravel 5.5+)

Register provider and facade on your config/app.php file.

'providers' => [
    ...,
    Yajra\DataTables\DataTablesServiceProvider::class,
]

'aliases' => [
    ...,
    'DataTables' => Yajra\DataTables\Facades\DataTables::class,
]

Configuration (Optional)

$ php artisan vendor:publish --provider="Yajra\DataTables\DataTablesServiceProvider"

And that's it! Start building out some awesome DataTables!

Debugging Mode

To enable debugging mode, just set APP_DEBUG=true and the package will include the queries and inputs used when processing the table.

IMPORTANT: Please make sure that APP_DEBUG is set to false when your app is on production.

PHP ARTISAN SERVE BUG

Please avoid using php artisan serve when developing with the package. There are known bugs when using this where Laravel randomly returns a redirect and 401 (Unauthorized) if the route requires authentication and a 404 NotFoundHttpException on valid routes.

It is advised to use Homestead or Valet when working with the package.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Comments
  • how to fix ('Undefined index: columns')

    how to fix ('Undefined index: columns')

    production.ERROR: exception 'ErrorException' with message 'Undefined index: columns' in vendor/yajra/laravel-datatables-oracle/src/yajra/Datatables/Engine/BaseEngine.php:919

    opened by jongkon 38
  • datatables laravel returned html element as a text

    datatables laravel returned html element as a text

    The laravel datatables return an HTML as a text I used a rawColumns and escapeColumns but not working

    Please what a solution and thank you

    Please find a below pictures dara dara2

    Code snippet of problem

    System details

    • Operating System
    • PHP Version
    • Laravel Version 5.3
    • Laravel-Datatables Version 7.9
    bug 
    opened by AbuHamdah 33
  • [8.0] Support for Eloquent API Resources

    [8.0] Support for Eloquent API Resources

    Hello,

    This PR aims to address the following issues:

    https://github.com/yajra/laravel-datatables/issues/1515 https://github.com/yajra/laravel-datatables/issues/1659 https://github.com/yajra/laravel-datatables/issues/1351

    Laravel 5.5 introduced to us Eloquent API resources where we could easily describe the format that we wanted for our API responses. This package currently does not support the new API resources. Through this PR, I have added a new engine to handle API Resources, updated the configuration and added a new test case to test the new functionality.

    We can use the package as below now:

    DataTables::of(App\Http\Resources\UserResource::collection(App\User::all()))->toJson();

    or

    datatables()->of(App\Http\Resources\UserResource::collection(App\User::all()))->toJson();

    or

    DataTables::resource(App\Http\Resources\UserResource::collection(App\User::all()))->toJson();

    or

    datatables->resource(App\Http\Resources\UserResource::collection(App\User::all()))->toJson();

    Please review and let me know if there are any corrections and/or enhancements that can be done here if this PR is considered to be merged.

    Thanks!

    for review help wanted need docs 
    opened by asahasrabuddhe 32
  • Class 'Yajra\Datatables\Facades\Datatables' not found

    Class 'Yajra\Datatables\Facades\Datatables' not found

    Hello, Why i get this error :

    Class 'Yajra\Datatables\Facades\Datatables' not found
    

    Cause i never update anything.

    My app.php is :

    Yajra\Datatables\DatatablesServiceProvider::class,
    'Datatables' => Yajra\Datatables\Datatables::class,
    

    How to fix that?

    opened by fanjavaid 32
  • [8.0] Support for plugin engine methods.

    [8.0] Support for plugin engine methods.

    As discussed in #1294

    I have implemented more dynamic/flexibel methods for creating DataTable engines instances, the changes are:

    • added support for macros in DataTables.php
    • added boot method in DataTablesServiceProvider to add macro's for all engine names listed in config/datatables.php. Engine names are converted to camelCase, so if you list an engine as mongodb-query the macro will be named mongodbQuery.
    • added canCreate and create methods in DataTableAbstract. With the canCreate method a DataTableEngine can indicate whether they can be create with the supplied parameters (see Collection for the most interesting use-case). The create method then returns an instance of the DataTable engine. With these 2 methods DataTable Engine plugins have full control over how they should be used. See the CollectionEngine again which required a special treatment in DataTables.php (with the conversion from array to Collection) but is now fully self sufficient ;)

    Due to the canCreate method the config.datatables.builders option has become redundant, see the new implementation of DataTables::make. Therefore I would like to propose to comment out the builder options in config/datatables.php by default and mark that as a 'advanced configuration' option. That way advanced users can still override the builder to engine mapping, but if eg an Engine would split support for one of its supported Builders to a different class the bulk of the users are not affected.

    Hacktoberfest 
    opened by pimlie 28
  • SearchPanes support

    SearchPanes support

    Summary of problem or feature request

    When including the SearchPane datatables plugin (https://datatables.net/blog/2020-01-13) it looks like there is no data rendered.. what is needed to implement this functionality inside Yajra?

    enhancement Hacktoberfest Search Panes 
    opened by ratwizzard 27
  • Using livewire/livewire components in a yajra/laravel-datatables table

    Using livewire/livewire components in a yajra/laravel-datatables table

    Note: This is not a bug report or an issue. Prior to trying it out myself, I searched for the keyword livewire in the issues and did not find what I was looking for. So I thought, I leave a hint for future readers with a similar question. This issue may be closed at any time.

    I always wanted to have dynamic components within my tables, but unfortunately with vuejs, the default option provided by Laravel, this isn't possible. The vuejs core parses the DOM once when loading the page and switches then to a virtual DOM which is unaware of changes in the actual DOM. Therefore, when returning vuejs components from a DataTable, they are not recognized by vuejs and are simply not activated. And the alternative, plain JavaScript, was never an option for me either.

    When learning about livewire/livewire, I thought it might be a suitable alternative for exactly this scenario. And it turns out, it is! (As long as you add a tiny but necessary configuration to your tables.)

    The only thing's I had to add to my DataTables configuration are the following two options:

    {
        deferLoading: true,
        drawCallback: function(settings) {
            if (window.livewire) {
                window.livewire.rescan();
            }
        }
    }
    

    The deferLoading option was necessary for me because I'm also using a self-written logic to save and restore the state of my tables, especially the column search state. Without the option, my tables performed three draw cycles when loading a page, which seemed to break the livewire logic (it happened quite fast, maybe that's why). If you are not using custom logic in the initComplete callback, you most likely don't need this option.

    The drawCallback is the actual sweetness needed to get things working. And it is straight forward as well. Whenever we finish drawing the table (which happens when loading new data, e.g. on search, sort or page change), we ask livewire to look for new components and initialize them. That's it. livewire will even make sure to subscribe to new Laravel Echo channels, if any of the newly recognized components listens for some events. Unfortunately though, it won't unsubscribe when components (i.e. table data) are replaced with new ones. For me, this isn't an issue though.

    livewire 
    opened by Namoshek 25
  • Export Buttons not working on a non-service implementation

    Export Buttons not working on a non-service implementation

    Hello,

    I have tried the Service Implementation, and it works pretty well on exporting table contents. But whenever i try using non service implementations, the export buttons don't work. It keeps redirecting to a JSON result page. please see output below..

    print-pdf-csv-excel-output

    Kindly assist me please cuz i'm pretty stuck.

    My Controller**

    controller

    My Script Declaration**

    script

    My View

    table

    The Output when ever i click on any of the export buttons: Print, CSV, PDF, EXCEL

    print-pdf-csv-excel-output

    not supported 
    opened by sabival89 25
  • How to sort and search on table with belongsTo in datatables as a service, using eager loading

    How to sort and search on table with belongsTo in datatables as a service, using eager loading

    Summary of problem or feature request

    I have a table where I keep a list of locations. These locations also have a user. I can use datatables as a service to see the name of the users. For this, I add an extra table with the following code:

    return datatables()
                ->eloquent($query)->addColumn(
                    'username',
                    function ($location) {
                        return $location->user->name;
                    }
    

    However, I would like to sort and search on the user name, but this is not possible. Is there an easy solution for me to solve this?

    System details

    • Operating System: Homestead / Ubuntu Linux
    • PHP Version: 7.3
    • Laravel Version: 5.8
    • Laravel-Datatables Version: v9.4.1
    opened by WimDeMeester 24
  • Added Closure support for filterColumn method

    Added Closure support for filterColumn method

    Hello,

    with the following PR I have added a closure support for filterColumn method as a more advanced and flexible way of customising things.

    Instead of writing like this:

    ->filterColumn('id', 'whereRaw', 'interface_strings.interface_string_id = ?', [$request->get('columns')[0]['search']['value']])
    

    we can now use like this ($keyword supplied to a closure is without wildcards - especially handy when we want to do an exact match):

    ->filterColumn('id', function($query, $keyword) {
        $query->where('interface_strings.interface_string_id', $keyword);
    })
    

    Exact match issues specified in #217, #191 should be covered by this PR.

    Looking forward!

    opened by linaspasv 23
  • how to search in a column generated by  addColumn?

    how to search in a column generated by addColumn?

    First, great job for this amazing package.

    I have a small problem:

    I added a new custom column in your demo file: .../eloquent/add-edit-remove-column, I want to search/filter for that column, but I get an error in browser when I input everything.

    Is there a solution to search/filter in a custom column and after that to add a search input text for every column in table, including for custom_column?

    Error in FireBug console:

      <span class="exception_message">SQLSTATE[42S22]: Column not found: 1054 Champ
     &#039;new_column&#039; inconnu dans where clause (SQL: select count(*) as aggregate from (select &#039
    ;1&#039; as row_count from `users` where (LOWER(`id`) LIKE %b% or LOWER(`name`) LIKE %b% or LOWER(`email
    `) LIKE %b% or LOWER(`created_at`) LIKE %b% or LOWER(`updated_at`) LIKE %b% or LOWER(`new_column`) LIKE
     %b%)) count_row_table)</span>
    

    Code from Controller:

        public function getAddEditRemoveColumnData()
        {
            $users = User::select(['id', 'name', 'email', 'password', 'created_at', 'updated_at']);
    
            return Datatables::of($users)
                                     ->addColumn('new_column' , 'ABC')
                                      ->make(true)
        }
    

    Code from Javascript:

        $('#users-table').DataTable({
            processing: true,
            serverSide: true,
            ajax: '{{ url("eloquent/add-edit-remove-column-data") }}',
            columns: [
                {data: 'id', name: 'id'},
                {data: 'name', name: 'name'},
                {data: 'email', name: 'email'},
                {data: 'created_at', name: 'created_at'},
                {data: 'updated_at', name: 'updated_at'},
                {data: 'new_column', custom: 'new_column', orderable: true, searchable: true}
            ]
        });
    
    
    opened by marioene 22
  • jQuery is not defined when loading via ES module

    jQuery is not defined when loading via ES module

    Summary of problem or feature request

    Datatable won't initialise with deferred Jquery loading as a module. Have found a fix if anyone needs it.

    Code snippet of problem

    import Datatables instead of require:-

    import $ from 'jquery'
    import JSZip from "jszip";
    import DataTable from 'datatables.net-bs5'
    import 'datatables.net-buttons-bs5'
    import 'datatables.net-buttons/js/buttons.html5'
    import 'datatables.net-buttons/js/buttons.print'
    
    window.$ = window.jQuery = $;
    DataTable.Buttons.jszip(JSZip);
    
    

    Results in:- jQuery is not defined on the injected script.

    System details

    • Operating System - Ubuntu
    • PHP Version 8.1
    • Laravel Version 9.x
    • Laravel-Datatables Version 10.2.1

    ###Solution

    In the blade file when injecting the script set the attribute type as module:-

    @push('scripts')
        {!! $dataTable->scripts(null, ['type'=>'module']) !!}
    @endpush
    

    Hope this helps someone.

    @yajra Still totally in love with this package, it's frigging awesome. Thank you :-)

    If I set the script attribute on the builder:-

    $builder->scripts(null, ['type' => 'module']);
    

    The html is created with