From 369715b7c79bbaf9ca2aafd99668ce0df698eb79 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 11 Nov 2016 20:30:26 -0800 Subject: [PATCH] Starter text search scope for activity report (This will be messy) --- app/Http/Controllers/ReportsController.php | 2 +- app/Models/Actionlog.php | 30 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php index 49c5da4b7b..52559f896c 100644 --- a/app/Http/Controllers/ReportsController.php +++ b/app/Http/Controllers/ReportsController.php @@ -311,7 +311,7 @@ class ReportsController extends Controller $activitylogs = Company::scopeCompanyables(Actionlog::with('item', 'user', 'target'))->orderBy('created_at', 'DESC'); if (Input::has('search')) { - $activity = $activity->TextSearch(e(Input::get('search'))); + $activitylogs = $activitylogs->TextSearch(e(Input::get('search'))); } if (Input::has('offset')) { diff --git a/app/Models/Actionlog.php b/app/Models/Actionlog.php index 21a1cb75d5..db3556c97f 100755 --- a/app/Models/Actionlog.php +++ b/app/Models/Actionlog.php @@ -45,6 +45,10 @@ class Actionlog extends Model return $this->morphTo('item')->withTrashed(); } + public function company() { + return $this->hasMany('\App\Models\Company', 'id','company_id'); + } + public function itemType() { @@ -129,4 +133,30 @@ class Actionlog extends Model ->orderBy('created_at', 'asc') ->get(); } + + /** + * Query builder scope to search on text for complex Bootstrap Tables API + * + * @param Illuminate\Database\Query\Builder $query Query builder instance + * @param text $search Search term + * + * @return Illuminate\Database\Query\Builder Modified query builder + */ + public function scopeTextSearch($query, $search) + { + $search = explode(' OR ', $search); + + return $query->where(function ($query) use ($search) { + + foreach ($search as $search) { + $query->where(function ($query) use ($search) { + $query->whereHas('company', function ($query) use ($search) { + $query->where('companies.name', 'LIKE', '%'.$search.'%'); + }); + })->orWhere('action_type', 'LIKE', '%'.$search.'%') + ->orWhere('note', 'LIKE', '%'.$search.'%'); + } + + }); + } }