Skip to content

Commit

Permalink
Build out search and add Model order scope
Browse files Browse the repository at this point in the history
  • Loading branch information
madd15 committed Jul 30, 2015
1 parent c513007 commit e66ea4d
Showing 1 changed file with 44 additions and 13 deletions.
57 changes: 44 additions & 13 deletions app/models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,7 @@ public function scopeDeleted($query)
{
return $query->whereNotNull('deleted_at');
}



/**
* Query builder scope to search on text
*
Expand All @@ -402,18 +401,50 @@ public function scopeDeleted($query)
* @return Illuminate\Database\Query\Builder Modified query builder
*/
public function scopeTextSearch($query, $search)
{


return $query->where(function($query) use ($search)
{
{
$search = explode('+', $search);

$query->where('name','LIKE','%'.$search.'%')
return $query->where(function($query) use ($search)
{
foreach ($search as $search) {
$query->whereHas('model', function($query) use ($search) {
$query->join('categories','models.category_id','=','categories.id')
->where('categories.name','LIKE','%'.$search.'%')
->orWhere('models.name','LIKE','%'.$search.'%');
})->orWhere(function($query) use ($search) {
$query->whereHas('assetstatus', function($query) use ($search) {
$query->where('name','LIKE','%'.$search.'%');
});
})->orWhere(function($query) use ($search) {
$query->whereHas('defaultLoc', function($query) use ($search) {
$query->where('name','LIKE','%'.$search.'%');
});
})->orWhere(function($query) use ($search) {
$query->whereHas('assigneduser', function($query) use ($search) {
$query->where('first_name','LIKE','%'.$search.'%')
->orWhere('last_name','LIKE','%'.$search.'%');
});
})->orWhere('name','LIKE','%'.$search.'%')
->orWhere('asset_tag','LIKE','%'.$search.'%')
->orWhere('serial','LIKE','%'.$search.'%');
});

}

->orWhere('serial','LIKE','%'.$search.'%')
->orWhere('order_number','LIKE','%'.$search.'%');
}
});
}

/**
* Query builder scope to order on model
*
* @param Illuminate\Database\Query\Builder $query Query builder instance
* @param text $order Order
*
* @return Illuminate\Database\Query\Builder Modified query builder
*/
public function scopeOrderModels($query, $order)
{
return $query->where(function($query) use ($order)
{
$query->join('models', 'assets.model_id', '=', 'models.id')->orderBy('models.name', $order);
});
}
}

0 comments on commit e66ea4d

Please sign in to comment.