PHP code example of mayoz / eloquent-filterable

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

    

mayoz / eloquent-filterable example snippets


"    "mayoz/eloquent-filterable": "~2.0"
}

 namespace App\Filters;

use Mayoz\Filter\Filter;

class PublishedFilter extends Filter
{
	/**
	 * The first executable filter clause.
	 *
	 * @param  mixed  $query
	 * @return void
	 */
	public function before($query)
	{
		$query->where('published', '=', 1);
	}
}

 namespace App\Filters;

use Mayoz\Filter\Filter;

class StatusActiveFilter extends Filter
{
	/**
	 * The last executable filter clause.
	 *
	 * @param  mixed  $query
	 * @return void
	 */
	public function after($query)
	{
		$query->where('status', '=', 'active');
	}
}

 namespace App;

use Illuminate\Database\Eloquent\Model;
use Mayoz\Filter\Filterable;

class Post extends Model
{
    use Filterable;

	/**
	 * The attributes that should be filter.
	 *
	 * @var array
	 */
	protected $filters = [
		'App\Filters\StatusActiveFilter',
		'App\Filters\PublishedFilter'
	];

	// other things...
}

$model = \App\Post::where('views', '>', 100)->get();

 namespace App\Filters;

use Auth;
use Mayoz\Filter\Filter;

class StatusActiveFilter extends Filter
{
	/**
	 * The last executable filter clause.
	 *
	 * @param  mixed  $query
	 * @return void
	 */
	public function after($query)
	{
		# assume, the `users` table has `role` field.
		if (array_get(Auth::user(), 'role') != 'administrator')
		{
			$query->where('status', '=', 'active');
		}
	}
}