PHP code example of astritzeqiri / laravel-searchable

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

    

astritzeqiri / laravel-searchable example snippets


// E.x. User.php
// add this before the class declaration
use AstritZeqiri\LaravelSearchable\Traits\Searchable;

// after the class declaration add this code snippet:
use Searchable;

// This gives you a list of users that match the name john
$users = App\User::search('John', ['name'])->get();

// if you want the search to be exact you pass a third attribute
$users = App\User::search('John', ['name'], true)->get();


class User extends Model
{
    // These are the default search fields.
    protected static $searchOn = ['first_name', 'last_name'];
    
}

// Now you can do this.
// That gives you the users with the first_name or last_name Doe
$users = App\User::search('Doe')->get();

// Of course if you give it the second attribute it ignores the model fields.
// Now it only searches on the first_name not the last_name
$users = App\User::search('Doe', ['first_name'])->get();




// Ex. You want to search users profile description which is contained in the profiles table,
// you can do that by giving for example profile_description asf field.
$users = App\User::search('Texas', ['profile_description'])->get();

// And then you'll have to declare a scope function on you User model for that field.
// The function has to be called 'scopeSearchOn' and your field on studly_case
// in this example it needs to be 'scopeSearchOnProfileDescription'
class User extends Model
{
    /**
     * Search on the users profile description.
     * 
     * @param  QueryBuilder $query
     * @param  string $search [the string that we are searching for]
     * @param  string $exact [if exact searching has been