mawuekom/laravel-macro-search

Laravel macro that help make search on eloquent model

v1.0.1 2021-06-24 19:40 UTC

This package is auto-updated.

Last update: 2024-11-25 04:17:40 UTC


README

This package is a macro service provider to make search on your eloquent model

Installation

You can install the package via composer:

composer require mawuekom/laravel-macro-search

Usage

Laravel

Go to config/app.php, and add this in the providers key

'providers' => [
    ...
    Mawuekom\MacroSearch\MacroSearchServiceProvider::class,
    ...
]

Lumen

Go to bootstrap/app.php, and add this in the specified key

// Add provider 
$app->register(Mawuekom\MacroSearch\MacroSearchServiceProvider::class);

Your model which inherits an eloquent model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'posts';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'post_id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title',
        'content'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [];
}

Use it to make search

use App\Models\Post;

Post::whereLike(['title', 'content'], 'Post title');

Once done, enjoy it

Report bug

Contact me on Twitter @ephraimseddor

License

The MIT License (MIT). Please see License File for more information.