Troj's avatar
Level 4

Set selected option in dropdown

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'));
    }

}
0 likes
4 replies
MichalOravec's avatar

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>
Troj's avatar
Level 4

@michaloravec Yeah i tried something like that but i can't figure out exactly how to do it,

city=2 is equal to the id in the cities table, and to the city_id in my deals table. This is what the url looks like when i filter the results.

https://mywebsite.test/search?category=&city=2&travel-period=&order=1

Does this help?

Please or to participate in this conversation.