foreach Laravel-5 <option select

孤人 提交于 2021-02-08 10:28:18

问题


I have tried a lot of variants of code and tried to find similar problem in other topics. So, i have table users where each user is having one city(stored as a number), and of course table city with id and name of city (40 cities is there). When real user open his profile settings page I want his city to be selected and and displayed in form. In this example in user table "Alex" is having city"2". In table city: id "2" and name_ru "cityB".

If i try this:

@foreach(App\City::get() as $city)
<option value='{{ $city->id }}'>{{ $city->name_ru }}</option>
@endforeach

It shows only cities, but I need result like this:

<option  value="1" > cityA</option>
<option selected value="2" > cityB </option>
<option value="3" > cityC </option>

So the question is - How to make SELECTED only one tag OPTION, which VALUE is equal to number of that city, which is stored in table users for "Alex".

I thought about this, but it shows nothing:

@foreach(App\City::get() as $city)
@if($user->city ==1)
<option selected value="1" > {{ $city->name_ru }}</option>
@elseif($user->city ==2)
<option selected value="2" > {{ $city->name_ru }}</option>
....
@endif
@endforeach

If I try this:

@foreach(App\City::get() as $city)
@if($user->city ==1)
<option selected value="1" > {{ $city->name_ru }}</option>
@endif
@endforeach

@foreach(App\City::get() as $city)
@if($user->city ==2)
<option selected value="2" > {{ $city->name_ru }}</option>
@endif
@endforeach

@foreach(App\City::get() as $city)
@if($user->city ==3)
<option selected value="3" > {{ $city->name_ru }}</option>
@endif
@endforeach

I get:

<option selected value="2" > cityA</option>
<option selected value="2" > cityB</option>
<option selected value="2" > cityC</option>

Help please


回答1:


Try this:

@foreach(App\City::get() as $city)
$selected = '';
if($city->id == 1)    // Any Id
{
    $selected = 'selected="selected"';
}
// $selected is initially empty and when the if criteria met it contains selected which make dropdown selected
<option value='{{ $city->id }}' {{$selected}} >{{ $city->name_ru }}</option>
@endforeach

Edited: fetching data from models like App\City::get() on view is not a good idea, instead fetch it on controller and pass it on to the view is the right way.




回答2:


You should not use Model Queries inside your view as it is a bad practice as you're not following MVC Pattern. You can try this:

Inside your controller:

class UsersController extends Controller {
    public function users() {
        $users = User::with('city')->get();
        $cities = City::all();

        return view('view_name', compact('users', 'cities'));
    }
}

and inside your blade view, you can use ternary operator like this:

@foreach($users as $user)

    <select>

        @foreach($cities as $city)

            <option value="{{ $city->id }}" {{ ($user->city->id == $city->id) ? 'selected' : '' }}>
                {{ $city->name }}
            </option>

        @endforeach

    </select>

@endforeach

Hope this helps!




回答3:


<select name="cities" id="cities">
<option value="">Select Cities</option>
@foreach($cities as $city){
<option value="{{$city->id}}{{($result->cityid==$city->id)?'SELECTED':''; ?>">{{$city->name}}</option>
}
@endforeach


来源:https://stackoverflow.com/questions/41101152/foreach-laravel-5-option-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!