PHP code example of craftcodery / laravel-searchable

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

    

craftcodery / laravel-searchable example snippets


namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use CraftCodery\Searchable\Searchable;

class User extends Model
{
    use Searchable;

    /**
     * Get the searchable data array for the model.
     *
     * @return array
     */
    public function toSearchableArray()
    {
        return [
            'columns' => [
                'users.name'  => 60,
                'users.email' => 60,
                'locations.city' => 40,
                'organizations.name' => 40
            ],
            'joins'   => [
                'locations' => [
                    'users.location_id',
                    'locations.id'
                ],
                'organizations' => [
                    // use commas to join on multiple columns, e.g. where
                    // organizations.id equals primary_org_id OR secondary_or_id
                    'users.primary_org_id,users.secondary_org_id',
                    'organizations.id'
                ]
            ],
            'groupBy' => 'users.id'
        ];
    }
}

$users = User::search('john')->get();

php artisan vendor:publish --tag=searchable-config