PHP code example of theupriser / laravel-searchable

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

    

theupriser / laravel-searchable example snippets


return [
    /*
    |--------------------------------------------------------------------------
    | Default match weight
    |--------------------------------------------------------------------------
    |
    | The weight of all searched words which match at least one of the
    | list of searchable attributes.
    | Defaults to 1.
    |
    */

    'default_match_weight' => 1,
];
bash
php artisan vendor:publish --provider="Maize\Searchable\SearchableServiceProvider" --tag="searchable-config"
 php


namespace App\Models;

use Maize\Searchable\HasSearch;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Facades\DB;

class Article extends Model
{
    use HasSearch;

    protected $fillable = [
        'id',
        'title',
        'body',
        'creator_name',
        'creator_surname',
    ];

    protected $casts = [
        'body' => 'array',
    ];

    /**
     * Get the model's searchable attributes.
     *
     * @return array
     */
    public function getSearchableAttributes(): array
    {
        return [
            'title' => 5, // Model attribute
            'body.en' => 2, // Single json key of a model attribute
            'tags.name', // Relationship attribute
            'tags.description.*', // All json keys of a relationship attribute
            DB::raw("CONCAT(creator_name, ' ', creator_surname)"), // Raw expressions are supported too
        ];
    }

    /**
     * Allows fetching the tags bound to current article instance
     *
     * @return BelongsToMany
     */
     public function tags(): BelongsToMany
     {
        return $this->belongsToMany(Tag::class)->withTimestamps();
     }
}