Giter VIP home page Giter VIP logo

laravel-search's Introduction

Laravel Search

Description

Search class app without installing vendors and providers. This search app will integerated to your Eloquent Models. Please check the usage section for more details.

How To Use

  1. Clone the Search.php file or you can download file from assets version.

  2. Put the Search.php file to your own app folder (Model folder)

  3. Call the Search App file on your controller file. use App\Search

  4. Instance the search class with a construct of your model that you want to search (Ex: Item)

  namespace App;
  
  use App\Search;
  use App\Item;
  
  class ItemController extends Controller {
      ...    
      
      public function search($query)
      {
        $item = new Item 
        $search = new Search($item)
            
        // search methods
        $result = $search->find($query)
                         ->result()
                         ->get();
        
        return $result // return the search result from item's model
      }

      ...
  }

Relationship Model

  1. If you want to search with relationship, you can just define what relation model you want to search.

  2. You must have the relationship model first (Ex: Supplyer) on your model (Ex: Item)

  namespace App;

  use Illuminate\Database\Eloquent\Model;
  
  class Item extends Model
  {
      ...
      public function supplyer() // this method name will be called on the relation array with a string variable
      {
          return $this->belongsTo(Supplyer::class);
      }
      ...
  }
  1. Then, define what column you want to search on relation columns.
  $relation = [
    'supplyer' => ['name', 'info']
    
    // this 'supplyer' name was caled from item's model above
  ];
  1. Then, you can add the $relation variable to the 'with' methods.
   $search->find($query)
          ->with($relation)
          ->result() 
          ->get();

Collaborate with Eloquent methods

Get all the result
   $search->find($query)
          ->result() 
          ->get();
Get all the search result with pagination
   $search->find($query)
          ->result() 
          ->paginate(5);
Get all the search result from 'withTrashed' method
   $search->find($query)
          ->result()
          ->withTrashed()
          ->get(); // you can also use the paginate method
Get all the search result from 'onlyTrashed' method
   $search->find($query)
          ->result()
          ->onlyTrashed()
          ->get();

** and many more eloquent methods **

Coded © Arif Ramadhani

laravel-search's People

Contributors

ariframadhani avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.