PHP code example of djl997 / laravel-search-query-builder

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

    

djl997 / laravel-search-query-builder example snippets


$search = 'Cesar';

// vanilla laravel
User::where('name', 'LIKE', '%'. $search .'%')->orWhere('bio', 'LIKE', '%'. $search .'%')->get();

// laravel with laravel-search-query-builder package
User::search(['name', 'bio'], $search)->get();

$search = 'Cesar';

User::search(['name', 'bio'], $search)->get();

$search = 'Cesar, Victoria';

User::search(['name', 'bio'], $search)->get();

User::search(['name', 'bio'], 'Cesar Victoria', '|')->get();

$search = 'Laravel is cool';

User::search(['bio'], $search)
    ->orWhereHas('posts', function($query) use ($search) {
        $query->search(['title', 'body'], $search);
    })
    ->get();

$search = 'Laravel is cool';

User::whereIn('role', ['author', 'reviewer'])
    ->where(function($query) { //see note below
        ->search(['bio'], $search)
        ->orWhereHas('posts', function($query) use ($search) {
            $query
                ->search(['title', 'body'], $search)
                ->whereNotNull('published_at');
        })
        ->orWhereHas('comments', function($query) use ($search) {
            $query->search(['body'], $search);
        })
    })
    ->withTrashed()
    ->orderBy('name')
    ->get();

$search = $request->input('search');
$columns = $request->input('userColumns');

User::search($columns, $search)->get();