yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.74k stars 862 forks source link

[Feature] Add support SearchBuilder #2648

Open vanthao03596 opened 3 years ago

vanthao03596 commented 3 years ago

SearchBuilder is new extension and really good. Can we add support this ?

yajra commented 3 years ago

Search builder extension only works on client-side. Will consider this once the serverSide implementation is available.

vanthao03596 commented 3 years ago

hey @yajra SearchBuilder support server-side processing in version 1.2

yajra commented 3 years ago

@vanthao03596 Can you please provide some links for reference?

vanthao03596 commented 3 years ago

Here https://editor.datatables.net/examples/extensions/searchBuilderTypes.html

hamzaFpco commented 3 years ago

+1

vanthao03596 commented 3 years ago

@yajra @hamzaFpco i made a gist with simple scope for SearchBuilder base on datatable. Hope it can help https://gist.github.com/vanthao03596/b49747bf5725a97d091f482d42c7c576

armadeas commented 2 years ago

@yajra @hamzaFpco i made a gist with simple scope for SearchBuilder base on datatable. Hope it can help https://gist.github.com/vanthao03596/b49747bf5725a97d091f482d42c7c576

hi, how to use this script?

vanthao03596 commented 2 years ago

@yajra @hamzaFpco i made a gist with simple scope for SearchBuilder base on datatable. Hope it can help https://gist.github.com/vanthao03596/b49747bf5725a97d091f482d42c7c576

hi, how to use this script?

Use this trait in your model

armadeas commented 2 years ago

thanks for sharing. I use DataTableScope

https://gist.github.com/armadeas/61d0ed9e846c3ede8afa3fc6cdc18a97

enaeim commented 2 years ago

@yajra @hamzaFpco i made a gist with simple scope for SearchBuilder base on datatable. Hope it can help https://gist.github.com/vanthao03596/b49747bf5725a97d091f482d42c7c576

Thank you very much. This is very good πŸ˜ŠπŸ˜ŠπŸ˜ŠπŸ‘πŸ‘β€

enaeim commented 2 years ago

thanks for sharing. I use DataTableScope

https://gist.github.com/armadeas/61d0ed9e846c3ede8afa3fc6cdc18a97

Hi. Thanks a lot. Why did you use the $columns parameter in the searchBuilderCondition method?

protected function searchBuilderCondition($query, $data, $columns)
{
    // . . .
}
armadeas commented 2 years ago

yes because I use relationships from several tables, so that the searchbuilder can work specifically on which columns from the table to filter

enaeim commented 2 years ago

thanks for sharing. I use DataTableScope

https://gist.github.com/armadeas/61d0ed9e846c3ede8afa3fc6cdc18a97

Thank you for your answer. I had another question. The following class is not defined in the scope you wrote. Where is this class?

use Yajra\DataTables\Contracts\DataTableScope;

class SearchBuilderScope implements DataTableScope
{
    // . . .
}

image image

armadeas commented 2 years ago

sorry if it's messy. Initially I made a scope like the following link https://datatables.yajrabox.com/services/scope This scope is included in the service button https://yajrabox.com/docs/laravel-datatables/master/buttons-installation

after that the contents are https://gist.github.com/armadeas/61d0ed9e846c3ede8afa3fc6cdc18a97 image

enaeim commented 2 years ago

Hello dear friends. @yajra @armadeas @vanthao03596 Another question. Sorry. How to add a relationship field to searchbuilder. For example, in my project, in the admin panel, there is a table containing all the admins of the site and there is no column as the their roles. But I want to search and find users who are operators in searchbuilder. How do I implement this using searchbuilder and yajra? I am new to datatables, yajra and jquery. I would be very grateful if you could guide me or introduce me to the tutorial link or in-depth tutorial on this. Thanks a lot.

humbertleonardo commented 1 year ago

Can anyone provide a working sample code? I did the whole process above but it still returns the error Class "App\DataTables\Scopes\SearchBuilderScope" not found I think I have a problem calling the scope in the Model

radz2k commented 1 year ago

@vanthao03596 '!contains' case block is missing.

vipertecpro commented 1 year ago

Can anyone provide a working sample code? I did the whole process above but it still returns the error Class "App\DataTables\Scopes\SearchBuilderScope" not found I think I have a problem calling the scope in the Model

Please take a look i managed to make it work

  1. run this php artisan datatables:scope SearchBuilder
  2. Now add all that code mentioned in the gist to the SearchBuilder class Screenshot 2023-01-17 234942
  3. Then add that scope to your controller method
    
    <?php

namespace App\Http\Controllers;

use App\DataTables\UsersDataTable; use App\DataTables\Scopes\SearchBuilder; use App\Helpers\Helper;

class UsersController extends Controller { /**

Followings are the things which make me stuck all day to make it all work

  1. In your datatable class to show search builder you need to add 'Q' to your dom, if you are using that like this Screenshot 2023-01-17 235314 Screenshot 2023-01-17 235433
  2. You might face "414 request-uri too large", i am using nginx ec2 instance so in my case i had to configure in the root of my nginx file nano /etc/nginx/nginx.conf like this

    http {
    
        ##
        # Basic Settings
        ##
        client_header_buffer_size 64k; // this
        large_client_header_buffers 4 64k; // and this one

I hope it helps someone.

Yuri-M1 commented 1 year ago

Can anyone provide a working sample code? I did the whole process above but it still returns the error Class "App\DataTables\Scopes\SearchBuilderScope" not found I think I have a problem calling the scope in the Model

Please take a look i managed to make it work

  1. run this php artisan datatables:scope SearchBuilder
  2. Now add all that code mentioned in the gist to the SearchBuilder class Screenshot 2023-01-17 234942
  3. Then add that scope to your controller method
<?php

namespace App\Http\Controllers;

use App\DataTables\UsersDataTable;
use App\DataTables\Scopes\SearchBuilder;
use App\Helpers\Helper;

class UsersController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function list(UsersDataTable $dataTable)
    {
        $pageData = [
          'who_am_i'  => ' I AM Awsom'
        ];
        return $dataTable->addScope(new SearchBuilder)->render('pages.users.index',$pageData);
    }
} 
  1. Set your js and css accordingly..I am using metronic 8 theme so in my config it look like this Screenshot 2023-01-21 191823 That's all....

Followings are the things which make me stuck all day to make it all work

  1. In your datatable class to show search builder you need to add 'Q' to your dom, if you are using that like this Screenshot 2023-01-17 235314 Screenshot 2023-01-17 235433
  2. You might face "414 request-uri too large", i am using nginx ec2 instance so in my case i had to configure in the root of my nginx file nano /etc/nginx/nginx.conf like this
http {

        ##
        # Basic Settings
        ##
        client_header_buffer_size 64k; // this
        large_client_header_buffers 4 64k; // and this one

I hope it helps someone.

Hi, I am trying to config SearchBuilder server-side into may Laravel app and I am facing a lot of troubles. Could you please help me ? Maybe a tutorial about how I can make it work.....

saaiful commented 1 year ago

If you want to use SearchBuilder with a server-side implementation, you may find this article helpful.

https://medium.com/@saaifulislam/implementing-searchbuilder-with-yajra-datatables-in-laravel-a-step-by-step-guide-39314791ac5b

Yuri-M1 commented 1 year ago

Hi Saiful,

Thank you for your e-mail.

Best Regards,

Yuri

De: Saiful Islam @.> Enviada em: domingo, 16 de abril de 2023 11:47 Para: yajra/laravel-datatables @.> Cc: Yuri Araujo @.>; Comment @.> Assunto: Re: [yajra/laravel-datatables] [Feature] Add support SearchBuilder (#2648)

If you want to use SearchBuilder with a server-side implementation, you may find this article helpful.

@.***/implementing-searchbuilder-with-yajra-datatables-in-laravel-a-step-by-step-guide-39314791ac5b

β€” Reply to this email directly, view it on GitHubhttps://github.com/yajra/laravel-datatables/issues/2648#issuecomment-1510403875, or unsubscribehttps://github.com/notifications/unsubscribe-auth/A25KZZJCZWU2O4A3DRHRINTXBQA5JANCNFSM46T2TNTQ. You are receiving this because you commented.Message ID: @.**@.>>

vipertecpro commented 1 year ago

If you want to use SearchBuilder with a server-side implementation, you may find this article helpful.

https://medium.com/@saaifulislam/implementing-searchbuilder-with-yajra-datatables-in-laravel-a-step-by-step-guide-39314791ac5b

Yes that's perfect keep updating that....

ale1981 commented 9 months ago

If you want to use SearchBuilder with a server-side implementation, you may find this article helpful.

https://medium.com/@saaifulislam/implementing-searchbuilder-with-yajra-datatables-in-laravel-a-step-by-step-guide-39314791ac5b

@saaiful Is it possible to get this working with model attributes set using accessors, the columns wouldn't exist in the DB so I obviously get an error from SQL.

I would like to be able to filter based on these fields using Search Builder.

Yuri-M1 commented 9 months ago

Hi,

Thank you for your e-mail. I will try it.

Best Regards

Yuri

De: ale1981 @.> Enviada em: terça-feira, 12 de dezembro de 2023 14:43 Para: yajra/laravel-datatables @.> Cc: Yuri Araujo @.>; Comment @.> Assunto: Re: [yajra/laravel-datatables] [Feature] Add support SearchBuilder (#2648)

If you want to use SearchBuilder with a server-side implementation, you may find this article helpful.

@.***/implementing-searchbuilder-with-yajra-datatables-in-laravel-a-step-by-step-guide-39314791ac5b

@saaifulhttps://github.com/saaiful Is it possible to get this working with model attributes set using accessors, the columns wouldn't exist in the DB so I obviously get an error from SQL.

I would like to be able to filter based on these fields using Search Builder.

β€” Reply to this email directly, view it on GitHubhttps://github.com/yajra/laravel-datatables/issues/2648#issuecomment-1852515599, or unsubscribehttps://github.com/notifications/unsubscribe-auth/A25KZZOVRV6LIHKN73IRRCTYJCJRZAVCNFSM46T2TNT2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBVGI2TCNJVHE4Q. You are receiving this because you commented.Message ID: @.**@.>>

saaiful commented 9 months ago

If you want to use SearchBuilder with a server-side implementation, you may find this article helpful. https://medium.com/@saaifulislam/implementing-searchbuilder-with-yajra-datatables-in-laravel-a-step-by-step-guide-39314791ac5b

@saaiful Is it possible to get this working with model attributes set using accessors, the columns wouldn't exist in the DB so I obviously get an error from SQL.

I would like to be able to filter based on these fields using Search Builder.

You cannot filter data directly based on an accessor when using Yajra DataTables with the Eloquent driver in Laravel. Accessors are used to modify or format data when it's retrieved from the database, but they are not used for filtering or querying data.

jesusdavidvaldivia commented 8 months ago

If you want to use SearchBuilder with a server-side implementation, you may find this article helpful. https://medium.com/@saaifulislam/implementing-searchbuilder-with-yajra-datatables-in-laravel-a-step-by-step-guide-39314791ac5b

@saaiful Is it possible to get this working with model attributes set using accessors, the columns wouldn't exist in the DB so I obviously get an error from SQL. I would like to be able to filter based on these fields using Search Builder.

You cannot filter data directly based on an accessor when using Yajra DataTables with the Eloquent driver in Laravel. Accessors are used to modify or format data when it's retrieved from the database, but they are not used for filtering or querying data.

U can try use whereHas with the filter without any modification, of course, is a whereHas by every related model:

Captura de pantalla 2024-01-10 a las 13 30 04

$model = \App\students ->whereHas("students.status",function($query) use ($r){ // the current model$sb = new SearchBuilder($r, $query, ["students.status.name"], ["students.status.name" => "name"]); // let pass relation columns, students.status.name not exists yet, then use the column name using replacement array $query = $sb->build(); // return the new build query }) ->with("students.status.name") // optional if u want to retry relation info too