PHP code example of johnylemon / laravel-searchable

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

    

johnylemon / laravel-searchable example snippets





use Johnylemon\Searchable\Traits\Searchable;

class User extends Model
{
    use Searchable;

    /**
     * Searchable properties array
     *
     * @return    array    searchables
     */
    protected function searchable(): array
    {
        return [
            'name',
            'email',
        ];
    }
}


ModelName::withSearch()->get();


Route::get('/users?name=John', function(){

    return ModelName::withSearch()->get();

});


use App\Search\CustomSearch;

/**
 * Searchable properties array
 *
 * @return    array    searchables
 */
public function searchable(): array
{

    return [

        // this field will be searched using `BasicSearch` class
        'last_name',

        // will use `CustomSearch` filter for `first_name` field
        'first_name' => CustomSearch::class,

        // instead of using custom class, you may use anonymous function
        'full_name' => function($query, $property, $value) {
            $query->where(DB::raw("first_name || ' ' || last_name"), $value);
        },

        // `name` field will be searched same way as `first_name`
        // so it will use `CustomSearch` class
        'name' => 'first_name',
    ];

}



use App\Search\SearchAsFirstName;
use App\Search\SearchAsAnotherField;

public function searchable(): array
{
    return [
        //
        // use callable
        //
        'name' => function($query, $property, $value) {
            $query->where('first_name', $value);
        },

        //
        // or custom class
        //
        'name' => SearchAsFirstName::class,

        //
        // or even search class instance
        //
        'name' => new SearchAsAnotherField('first_name'),
    ];
}



public function searchable(): array
{
    return [
        'ghost' => function($query, $property, $value) {
            $query->whereNotNull('deleted_at');
        },
    ];
}



'shorthands' => [
    'ghost' => App\Search\GhostUsers::class,
    'awesomness' => App\Search\AwesomeSearchFilter::class,
],



//
// config file
//
`shorthands` => [
    'name' => App\Search\NameSearch::class,
],

//
// model searchable method
//
public function searchable(): array
{
    return [
        'name',
        'username' => 'name', // `name` will be treated as shorthand, so `App\Search\NameSearch` search filter will be used  
    ];
}


public function searchable(): array
{
    return [
        //  will be searched using `%LIKE%` provided by `LikeSearch` class
        'email' => 'like',

        //  will be searched using `LIKE%` provided by `LikeBeginSearch` class
        'code' => 'like-begin',

        //  will be searched using `%LIKE` provided by `LikeEndSearch` class
        'suffix' => 'like-end',
    ];
}


php artisan vendor:publish

GET /users?name=John&nick=johnylemon


php artisan searchable:generate MySearch