Level 75
I just compare city id with 1, because I don't know with what you want to compare it.
<option value="{{ $city->id }}" {{ $city->id == 1 ? 'selected' : '' }}>{{ $city->name }}</option>
How can i set the selected option in my dropdown? I think i have to compare the city->id value with something, but i cant figure out how. Can someone help me with it?
<select name="city" class="pl-10 text-base sm:text-sm mt-1 form-select block w-full text-gray-500 focus:bg-gray-100">
<option value=''>{{__('Search cities...')}}</option>
@foreach($cities as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
@endforeach
</select>
class SearchController
{
public function index(Request $request)
{
$deals = Deal::query();
if($request->get('category')) {
$deals->where('category_id', '=', $request->get('category'));
}
if($request->get('city')) {
$deals->where('city_id', '=', $request->get('city'));
}
$deals = $deals->get();
return view('search.index', compact('deals'));
}
}
So it looks like you want to compare it with a query string.
<option value="{{ $city->id }}" {{ $city->id == request()->query('city') ? 'selected' : '' }}>{{ $city->name }}</option>
Docs: https://laravel.com/docs/8.x/requests#retrieving-input-from-the-query-string
Please or to participate in this conversation.