Jade Bird

Jade Bird

1568619210

Laravel Eloquent Filter Tutorial for Beginners

This simple package helps you filter Eloquent data using query filters.     

Installation

Run the following command:

composer require nahidulhasan/eloquent-filter 

Getting started

Use the trait NahidulHasan\EloquentFilter\Filterable in your eloquent model.

Create a new class by extending the class NahidulHasan\EloquentFilter\QueryFilters and define your custom filters as methods with one argument. Where function names are the filter argument name and the arguments are the value.

Let's assume you want to allow to filter articles data. Please see the following code.

<?php  
namespace App\Models;  

use Illuminate\Database\Eloquent\Model;
use NahidulHasan\EloquentFilter\Filterable;

class Article extends Model
{
use Filterable;

/**

  • The attributes that are mass assignable.
  • @var array
    */
    protected $fillable = [ ‘title’, ‘body’ ];
    }

Create ArticleFilter class extending QueryFilters.

<?php
namespace App\Filters;

use Illuminate\Database\Eloquent\Builder;
use NahidulHasan\EloquentFilter\QueryFilters;

class ArticleFilters extends QueryFilters
{

/**

  • Filter by Title.
  • @param $title
  • @return Builder
  • @internal param $name
  • @internal param string $level
    */
    public function title($title) {
    return $this->builder->where(‘title’, ‘like’, ‘%’ .$title.‘%’);
    }
    }

With this class we can use the http query string : title=article_name or any combination of these filters. It is up to you to define if you will use AND wheres or OR.

Now in the controller you can apply these filters like as described in below :

<?php
namespace App\Http\Controllers;

use App\Filters\ArticleFilters;
use App\Models\Article;
use Illuminate\Http\Request;

/**

  • Class ArticleController
  • @package App\Http\Controllers
    /
    class ArticleController extends Controller
    {
    /
    *
  • Display a listing of the resource.
  • @param ArticleFilters $filters
  • @return \Illuminate\Http\Response
  • @internal param Request $request
    */
    public function index(ArticleFilters $filters)
    {
    $articles = Article::filter($filters)->paginate(5);
    return view(‘articles.index’,compact(‘articles’)) ->with(‘i’, (request()->input(‘page’, 1) - 1) * 5);
    }
    }

If you go to this link you will get all code: https://github.com/nahidulhasan/laravel-eloquent-query-filtering

Thanks to : https://github.com/laracasts/Dedicated-Query-String-Filtering

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Further reading

What’s New in Laravel 6.0

Laravel 6 CRUD Application Tutorial

Laravel 6 Image Upload Tutorial

Laravel 6 Authentication Tutorial

#laravel #php #web-development

What is GEEK

Buddha Community

Laravel Eloquent Filter Tutorial for Beginners

I am Developer

1617089618

Laravel 8 Tutorial for Beginners

Hello everyone! I just updated this tutorial for Laravel 8. In this tutorial, we’ll go through the basics of the Laravel framework by building a simple blogging system. Note that this tutorial is only for beginners who are interested in web development but don’t know where to start. Check it out if you are interested: Laravel Tutorial For Beginners

Laravel is a very powerful framework that follows the MVC structure. It is designed for web developers who need a simple, elegant yet powerful toolkit to build a fully-featured website.

Recommended:-Laravel Try Catch

#laravel 8 tutorial #laravel 8 tutorial crud #laravel 8 tutorial point #laravel 8 auth tutorial #laravel 8 project example #laravel 8 tutorial for beginners

I am Developer

1609729452

Laravel 8 Tutorial For Beginners Step by Step

Recommended:- Laravel Try Catch

#laravel #laravel 8 tutoral #laravel 8 tutorial for beginners #laravel 8 tutorial for beginners step by step #laravel 8 tutorial from scratch

I am Developer

1605329413

Laravel 8 Tutorial For Beginners

In this tutorial, i will provide you some useful tutorial of laravel 8 version. So, you can learn laravel 8 an easy way.

Recommended:- Laravel Eloquent whereRaw Query Example
Recommended:- How to Get Random Records in Laravel
Recommended:- Laravel InsertOrIgnore Example
Recommended:- Laravel whereIn, whereNotIn With SubQuery Example
Recommended:- Laravel Where Null and Where Not Null Query
Recommended:- Laravel Group by Example
Recommended:- Laravel Order by Example
Recommended:- Laravel 8 Joins Example Tutorial
Recommended:- Laravel 8 – Form Validation Example
Recommended:- Laravel 8 Ajax Post Form Data With Validation
Recommended:- Laravel 8 Flash Message Example Tutorial
Recommended:- Laravel 8 Auth Scaffolding using Jetstream
Recommended:- Laravel 8 Autocomplete Search from Database Tutorial
Recommended:- How to Create Controller, Model in Laravel 8 using cmd
Recommended:- How to Use Helper Function in Laravel 8
Recommended:- Laravel 8 Send Mail using Queue Tutorial
Recommended:- Laravel 8 Google Recaptcha V3 Example
Recommended:- Laravel 8 QR Code Generator Tutorial Example
Recommended:- Laravel 8 Image Upload Tutorial
Recommended:- Laravel 8 Ajax Image Upload with Preview Tutorial
Recommended:- Laravel 8 Ajax Multiple Image Upload Tutorial
Recommended:- Laravel 8 FullCalendar Ajax Tutorial Example
Recommended:- Laravel 8 Livewire File Upload Tutorial Example
Recommended:- Laravel 8 Login with Linkedin Example Tutorial
Recommended:- Laravel 8 Multi Auth (Authentication) Tutorial
Recommended:- Laravel 8 Rest API with Passport Tutorial
Recommended:- Laravel 8 JWT Rest API Authentication Example Tutorial
Recommended:- Laravel 8 Datatables with Relationship Tutorial Example
Recommended:- Laravel 8 Joins Example Tutorial
Recommended:- Laravel 8 Summernote Image Upload Tutorial Example
Recommended:- Laravel 8 Crop Image Before Upload using Cropper JS
Recommended:- Laravel 8 – Dynamically Multiple Add or Remove Input Fields using jQuery
Recommended:- Laravel 8 PHP Guzzle Http Client GET & POST Example
Recommended:- Laravel 8 Livewire Datatables Tutorial Example
Recommended:- Laravel 8 Google Chart Tutorial Example
Recommended:- Laravel 8 Generate Fake Data
Recommended:- Laravel 8 Livewire Load More OnScroll Tutorial Example
Recommended:- Laravel 8 Dynamic Dependent Dropdown using Ajax
Recommended:- Laravel 8 Auto Load More Data On Page Scroll
Recommended:- Laravel 8 Simple CRUD Example Tutorial
Recommended:- Laravel 8 Rest API CRUD with Passport Auth Tutorial
Recommended:- Laravel 8 DataTable CRUD Tutorial
Recommended:- Laravel 8 Ajax CRUD Using Datatable Tutorial
Recommended:- Laravel 8 Ajax CRUD with Image Upload Tutorial
Recommended:- Laravel 8 Livewire CRUD with Jetstream Tutorial

#laravel 8 tutorial for beginners #laravel 8 tutorial for beginners step by step #laravel 8 tutorial #laravel 8 authentication tutorial

Marcelo Kapi

1588841272

Laravel 7 Tutorial for Beginner: Create your First To-Do App

In this Laravel tutorial, we will learn how to install laravel and how to create your first app in laravel for beginners. Now you can learn laravel easily without the need of watching laravel video series or laravel video tutorials. This laravel tutorial will explain all the necessary concepts in easy language for you to learn laravel easily and understand it better.

#Laravel #laravel tutorial #laravel tutorial for beginner #learn laravel #learn laravel for free

mehul bagada

mehul bagada

1593747985

Laravel Google Bar Charts Example Tutorial

Hi Guys,

In this tutorial.I will learn you how to use dynamic char bar using google charts in laravel.you can easy and simply use dynamic chart in laravel.

Google has provide us different type of Google chart. So, in this post we are going to create dynamic bar chart by using Google Chart API in Laravel application.

I will just create a simple employee model and a employee controller to create this laravel google charts example. Then i will fetch employee data from database and finally set in google bar chart function.

Link :- https://www.nicesnippets.com/blog/laravel-google-bar-charts-example-tutorial

#laravel #laravel tutorial #laravel tutorial for beginner #learn laravel #learn laravel for free