PHP code example of heseya / laravel-searchable

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

    

heseya / laravel-searchable example snippets


class User extends Model
{
    use HasCriteria;

    protected $criteria = [
        'id', // default criterion
        'email' => Equals::class,
        'name' => Like::class,
        'description' => Custom::class,
    ];
}

class Controller
{
    function index(Request $request)
    {
        User::searchByCriteria([
            'email' => '[email protected]',
            'name' => 'John'
        ])->get();

        // you can extend query

        User::searchByCriteria($request->all())
            ->where('public', true)
            ->get();
    }
}

final class CustomCriterion extends Criterion
{
    public function query(Builder $query): Builder
    {
        // do whatever you want

        return $query;
    }
}


class User extends Model
{
    protected function getDefaultCriterion(): string
    {
        return Equals::class;
    }
}