PHP code example of leandrogrando / simple-searchable

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

    

leandrogrando / simple-searchable example snippets


use LeandroGrando\SimpleSearchable\SimpleSearchable;

class User extends Model
{
    use SimpleSearchable;

    /**
     * Searchable fields.
     *
     * @var array
     */
    protected $searchable = [
        'name',
        'email',
        'city.name' // Field in a relationship
        'city.status.name' // Field in a multi relationship
    ];

    public function city()
    {
        return $this->belongsTo('City::class');
    }

}

// Simple search
// $query = 'my search term'
$users = User::search($query)->get();

// Search and get relations
// It will not get the relations if you don't do this
$users = User::search($query)
            ->with('city.state')
            ->get();

// Search with relations and paginate
$users = User::search($query)
            ->with('city.state')
            ->paginate(20);

// Search only active users
$users = User::where('status', 'active')
            ->search($query)
            ->paginate(20);


$users = User::search($query, [
                'city.name',
                'city.state.name'
                'city.state.uf'
            ])-get();