2017 © Pedro Peláez
 

library metasearch_l5

image

elfif/metasearch_l5

  • Friday, November 24, 2017
  • by elfif
  • Repository
  • 1 Watchers
  • 0 Stars
  • 130 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 14 % Grown

The README.md

Metasearch_l5

Overview

Metasearch is a package for Laravel 5. It allows you to easily create an Eloquent query that match with a search form. By using a list of keywords on your search form input fields names, Metasearch_l5 will automatically create the corresponding query . It's more or less a port of activerecord-hackery/meta_search. Right n now i'm using it a lot in Rest web services, as it allows great flexibility when you need some search capabilities., (*1)

Here is a simple example :, (*2)

first the search form :, (*3)

{{ Form::open(['url' => URL::route('cars.search')]) }}
    {{ Form::label('year : ') }}
    {{ Form::text('year_equals') }}

    {{ Form::label('minumum horsepower : ') }}
    {{ Form::text('horsepower_greater_than') }}

    {{ Form::label('maximum horsepower : ') }}
    {{ Form::text('horsepower_less_than') }}

    {{ Form::submit('submit') }}
{{ Form::close() }}

Then in your CarsController :, (*4)

    $input = Request::all();
    $query = Search::getQuery(Car::getQuery(), $input);
    $cars = $query->get();
    return view('cars.index', ['cars' => $cars]);

And that's it, the getQuery method will create the Query based on the Input content., (*5)

Installation

first add the dependency to uyour composer.json file :, (*6)

    "require": {
        "elfif/MetaSearch_L5" : "*"
    }

and do a composer install., (*7)

Then edit your app.php file. Add the service provider for this package :, (*8)

    'Elfif\MetaSearch\MetaSearchServiceProvider'

and add the facade :, (*9)

    'Search' => 'Elfif\MetaSearch\Facades\Search'   

Now you are good to go !!, (*10)

Syntax

Here is a list of all the keywords you can use with LaraSearch, based on their datatypes, (*11)

All data types

  • equals_ (alias: _eq) - Just as it sounds.
  • _does_not_equal_ (alias : _noteq) - The opposite of equals, oddly enough.
  • is_in - Takes an array, matches on equality with any of the items in the array.
  • is_not_in - Like above, but negated.
  • is_null - The column has an SQL NULL value.
  • is_not_null - The column contains anything but NULL.

Strings

  • _contains - Substring match.
  • _does_not_contain - Negative substring match.
  • _starts_with (alias: _sw) - Match strings beginning with the entered term.
  • _does_not_start_with (alias: _dnsw) - The opposite of above.
  • _ends_with (alias: _ew) - Match strings ending with the entered term.
  • _does_not_end_with (alias: _dnew) - Negative of above.

Numbers, dates, and times

  • _greater_than (alias: _gt) - Greater than.
  • _greater_than_or_equal (alias: _gteq) - Greater than or equal to.
  • _less_than (alias: _lt) - Less than.
  • _less_than_or_equal (alias: _lteq) - Less than or equal to.

Booleans

  • _is_true - Is true. Useful for a checkbox like "only show admin users".
  • _is_false - The complement of _is_true.

Non-boolean data types

  • _is_present - As with _is_true, useful with a checkbox. Not NULL or the empty string.
  • _is_blank - Returns records with a value of NULL or the empty string in the column.

ORed conditions

If you'd like to match on one of several possible columns, you can do this:, (*12)

    {{ Form::label('name : ') }}
    {{ Form::text('name_or_brand_equals') }}

Just keep in mind the condition will be the same for all columns., (*13)

Querying relation existence

Simple case

You can also check for a relation existence using the keyword "exist" like that. Let's say we have a relation called accessories in our car model :, (*14)

    public function accessories(){
        return $this->hasMany('App\Accessory');
    }

We can request that relation's existence using the keyword "_exists" (or _ex in short) this way :, (*15)

    {{ Form::label('has accessories : ') }}
    {{ Form::checkbox('accessories_exists') }}

If the checkbox is checked it will add that condition to the query builder, (*16)

    ->has('accessories');
With a count

You may also specify an operator and count to further customize the query using the keyword "count" (or _co in short) plus a keyword to define the condition, (*17)

    {{ Form::label('has accessories : ') }}
    {{ Form::text('accessories_count_greater_than') }}

Will translate into :, (*18)

    ->has('accessories', '>=', $value);
Full condition

If you need even more control over your conditions regarding a relation you can use that notation, with a dot between the relation and the field's name, followed by a condition, (*19)

    {{ Form::label('accessories : ') }}
    {{ Form::text('accessories.type_contains') }}

It will translate into :, (*20)

    ->whereHas('accessories', function($query){
        return $query->where('type', 'like', '%'.$value.'%');
    });

The Versions

24/11 2017

dev-master

9999999-dev

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

by Philippe MICHEL

27/09 2016

dev-has_count

dev-has_count

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

by Philippe MICHEL