1. Go to this page and download the library: Download baril/sqlout 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/ */
namespace App\Models;
use Baril\Sqlout\Searchable;
class Post extends Model
{
use Searchable;
protected $weights = [
'title' => 4,
'excerpt' => 2,
];
public function toSearchableArray()
{
return [
'title' => $this->post_title,
'excerpt' => $this->post_excerpt,
'body' => $this->post_content,
];
}
// Optionally, you can customize the
// name of the table that the model
// will be indexed in:
public function searchableAs(): string
{
return 'my_custom_index';
}
}
// Restrict the search to some fields only:
$builder->only('title');
$builder->only(['title', 'excerpt']);
// (use the same names as in the toSearchableArray method)
// Retrieve the total number of results:
$nbHits = $builder->count();
$results = Post::search('you see what happens larry')
->published() // the `published` scope is defined in the Post class
->where('date', '>', '2010-10-10')
->get();
$results = Post::search('ve vant ze money lebowski')
->query(function ($query) {
$query->within('something');
})
->get();