Skip to content

Commit

Permalink
Add query to search into the per-component view
Browse files Browse the repository at this point in the history
  • Loading branch information
inietov committed Aug 15, 2023
1 parent 7eaf317 commit 993918f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/Http/Controllers/Api/ComponentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Events\ComponentCheckedIn;
use App\Models\Asset;
use Illuminate\Support\Facades\Validator;
use Illuminate\Database\Query\Builder;

class ComponentsController extends Controller
{
Expand Down Expand Up @@ -210,6 +211,21 @@ public function getAssets(Request $request, $id)
$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);
})
->get();
$total = $assets->count();
}

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

Expand Down

0 comments on commit 993918f

Please sign in to comment.