PHP code example of makidizajnerica / laravel-searcher

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

    

makidizajnerica / laravel-searcher example snippets




use Illuminate\Database\Eloquent\Model;
use MakiDizajnerica\Searcher\Searchable;
use MakiDizajnerica\Searcher\Contracts\Searchable as SearchableContract;

class Post extends Model implements SearchableContract
{
    use Searchable;

    /**
     * Get model attributes that will be used for tags.
     * 
     * @return array<int, string>
     */
    public function attributesForTags(): array
    {
        return $this->only(['title']);
    }
}



use App\Models\Post;

$post = Post::createWithoutTags(['title' => 'Test']);



use App\Models\Post;

$post = Post::first();

// Example 1
$post->updateWithoutTags(['title' => 'New Test']);

// Example 2
$post->title = 'New Test';
$post->saveWithoutTags();



use App\Models\Post;

$post = Post::first();

$post->withoutTags()->delete();



use Illuminate\Database\Eloquent\Model;
use MakiDizajnerica\Searcher\Searchable;
use MakiDizajnerica\Searcher\Contracts\Searchable as SearchableContract;

class Post extends Model implements SearchableContract
{
    use Searchable;

    /**
     * @var string
     */
    protected $searchType = 'news';
}



use App\Models\Post;

class PostController extends Controller
{
    public function index(Request $request)
    {
        return view('post.index', [
            'posts' => Post::whereTags($request->query('search'))->paginate()
        ]);
    }
}



use App\Models\User;
use App\Models\Post;
use MakiDizajnerica\Searcher\Search;
use Illuminate\Database\Eloquent\Builder;

class PostController extends Controller
{
    public function index(Request $request)
    {
        return view('post.index', [
            'search' => (new Search)
                ->addModel(User::class, function (Builder $query) {
                    $query->active()->notAdministrator();
                })
                ->addModel(Post::class, ['latest', 'limit' => 10])
                ->search($request->query('search'))
        ]);
    }
}



use App\Models\Post;
use MakiDizajnerica\Searcher\Search;

(new Search)->addModel(Post::class, 'latest')->search('test', true);

@foreach($search as $type => $results)
    <p>
        {{ $type }}
    </p>

    @foreach($results as $result)
        <div>
            <h1>
                {{ $result->title }}
            </h1>
        </div>
    @endforeach
@endforeach