kevinpijning / laravel-searchable by kevinpijning

Easy table searching for Laravel
1,316
2
2
Package Data
Maintainer Username: kevinpijning
Maintainer Contact: kevin@pijning.nl (Kevin Pijning)
Package Create Date: 2017-05-04
Package Last Update: 2017-06-28
Language: PHP
License: MIT
Last Refreshed: 2024-11-23 03:16:44
Package Statistics
Total Downloads: 1,316
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 2
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Searchable Laravel (5.*)

This package let you easily search through models by a Searchable trait.

Setup

Composer

Pull this package in through Composer

{
    "require": {
        "kevinpijning/laravel-searchable": "dev-master"
    }
}

$ composer update

Add the package to your application service providers in config/app.php

'providers' => [

    App\Providers\RouteServiceProvider::class,

    /*
     * Third Party Service Providers...
     */
     KevinPijning\LaravelSearchable\LaravelSearchableServiceProvider::class,
],

Publish the view:

php artisan vendor:publish

Usage

Use Searchable trait inside your Eloquent model(s). Define $searchable array (see example code below).

use KevinPijning\LaravelSearchable\Searchable;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract 
{
	use Authenticatable, CanResetPassword, Searchable;
	...

	public $searchable = ['id',
	                    'name',
	                    'email',
	                    'created_at',
	                    'updated_at'];
	                    
	...
}

Searchable trait adds Searchable scope to the models so you can use it with paginate.

Blade extension

There is one blade extension for you to use @searchableform

@searchableform

This will include a search form to your page.

Full example

Routes

Route::get('users', ['as' => 'users.index', 'uses' => 'HomeController@index']);

Controller

use App\User;

public function index()
{
    $users = User::searchable()->paginate(10);
    
    return view('user.index')->withUsers($users);
}

View

Pagination included

@searchableform

@foreach($users as $user)
	{{ $user->name }}
@endforeach

{!! $users->appends(\Request::except('page'))->render() !!}