PHP code example of illusi03 / lara-search

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

    

illusi03 / lara-search example snippets


Illusi03\LaraSearch\ServiceProvider::class,

'LaraSearch' => Illusi03\LaraSearchFacade::class,

use Illusi03\LaraSearch\Traits\LaraSearchable;

class Model extends Eloquent {

    use LaraSearchable;

    ...
}

$posts = Post::laraSearch($userInput, ['title'], [
    'comments' => 'comment',
    'comments.user' => ['name', 'lastname']
])->get();

$searchSchema = [
    'fields' => ['title'], // Fields where you want to search in the main model
    'relationships' => [ // Relationships, if any
        [
            'relationship' => 'comments', // Here you put name of the relationship
            'fields' => 'comment', // And here the fields where you want to search in the related table
        ],
        [
            'relationship' => 'comments.user', // Use dot notation for inner relations
            'fields' => ['name', 'lastname'],
        ]
    ]
];

$search = LaraSearch::find($userInput, App\Models\Post::query(), $searchSchema)->where('active', 1)->paginate(10);