PHP code example of tasdan / column-searchable

1. Go to this page and download the library: Download tasdan/column-searchable 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/ */

    

tasdan / column-searchable example snippets


'providers' => [
    // ...
    /**
     * Third Party Service Providers...
     */
    Tasdan\ColumnSearchable\ColumnSearchableServiceProvider::class,
    // ...
],

use Tasdan\ColumnSearchable\Searchable;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword, Searchable;
    // ...

    public $searchable = [
        'search_field_name_1' => [
            'relation' => 'relation_table_name',
            'columns' => 'column_name',
            'type' => 'string/int'
        ],
        'search_field_name_2' => [
            'relation' => 'relation_table_name',
            'columns' => 'column_name',
            'type' => 'string/int'
        ],
    // ...
}

 $users = User::searchable()->paginate(15);

 public $searchable = [
        //search in name column in current table with string type 
        'name',

        //search in selected columns with OR relation in current table with string type
        'address' => [
            'columns' => [
                'country',
                'city',
                'zip_code',
                'street'
            ]
        ],
        
        //search in activation_code column in current table with string type, but the search field name is activation
        'activation' => [
            'columns' => 'activation_code'
        ],
       
        //search in is_active column in current table with int type
        'is_active' => [
            'type' => 'int'
        ],
        
        //search in a belongs-to related table's name column, with string type
        'school_name' => [
            'relation' => 'school',
            'columns' => 'name',
            'type' => 'string'
        ],
    
        //search in a belongs-to related table's city and street column with string type'
        'school_address' => [
            'relation' => 'school',
            'columns' => [
                'city',
                'street'
            ],
            'type' => 'string'
        ],   

        //search in a has-many related table's firstname and lastname column with string type
        'student_name' => [
            'relation' => 'students',
            'columns' => [
                'firstname',
                'lastname'
            ],
            'type' => 'string'     
        ]     
    ];
sh
php artisan vendor:publish --provider="Tasdan\ColumnSearchable\ColumnSearchableServiceProvider" --tag="config"
config/columnsearchable.php