php - Two layer conditional queries in Laravel -


i'm new laravel , have been searching through documentation , haven't found answer fits need do.

i'm trying run flexible search query on products database.

so people can search name or sku ect. don't can leave fields blank.

so when input null replace "no(field)entered".

my search query has first check if field null, if not, needs filter parameter given.

a quick sql representation of i'm doing this.

$results = db::connection('sqlsrv')->select("select top 500 * dbo.simpleorderview  ('". $name ."' = 'nonameentered' or name '%". $name ."%')  , ('". $sku ."' = 'noskuentered' or sku '%". $sku ."%')  , ('". $description ."' = 'nodescriptionentered' or shortdescription '%". $description ."%')"); 

so query messy, works expected. i'd use eloquent clean , make perform way supposed to.

thanks in advance.

public function search(request $request) {     $inputs = $request->only('name', 'sku', 'description');     $query = class:take(500);      foreach ($inputs $key => $value)     {       $new_key = ucfirst($key);       $new_value = "%{$value}%";       if (empty($input))           $query->where("no{$new_key}entered", 'like', $new_value);       else           $query->where($new_key, $new_value);     }      return $query->get(); } 

this should trick


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -