Skip to content

Commit

Permalink
Move a couple assignation of variables inside an else to only execute…
Browse files Browse the repository at this point in the history
… once
  • Loading branch information
inietov committed Aug 16, 2023
1 parent 993918f commit 92df32d
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions app/Http/Controllers/Api/ComponentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,26 +204,28 @@ public function getAssets(Request $request, $id)
$this->authorize('view', \App\Models\Asset::class);

$component = Component::findOrFail($id);
$assets = $component->assets();


$offset = request('offset', 0);
$limit = $request->input('limit', 50);
$total = $assets->count();
$assets = $assets->skip($offset)->take($limit)->get();


if ($request->filled('search')) {
$assets = $component->assets()
->where(function ($query) use ($request) {
$search_str = '%' . $request->input('search') . '%';
$query->where('name', 'like', $search_str)
->orWhereIn('model_id', function (Builder $query) use ($request) {
$search_str = '%' . $request->input('search') . '%';
$query->selectRaw('id')->from('models')->where('name', 'like', $search_str);
})
->orWhere('asset_tag', 'like', $search_str);
->where(function ($query) use ($request) {
$search_str = '%' . $request->input('search') . '%';
$query->where('name', 'like', $search_str)
->orWhereIn('model_id', function (Builder $query) use ($request) {
$search_str = '%' . $request->input('search') . '%';
$query->selectRaw('id')->from('models')->where('name', 'like', $search_str);
})
->orWhere('asset_tag', 'like', $search_str);
})
->get();
$total = $assets->count();
} else {
$assets = $component->assets();

$total = $assets->count();
$assets = $assets->skip($offset)->take($limit)->get();
}

return (new ComponentsTransformer)->transformCheckedoutComponents($assets, $total);
Expand Down

0 comments on commit 92df32d

Please sign in to comment.