foreach Laravel-5 <option select
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41101152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
foreach Laravel-5 <option select
提问by Vit
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".
我尝试了很多代码变体,并试图在其他主题中找到类似的问题。所以,我有表用户,其中每个用户都有一个城市(存储为一个数字),当然还有带有 id 和城市名称的表城市(有 40 个城市)。当真实用户打开他的个人资料设置页面时,我希望他的城市被选中并以表格形式显示。在此示例中,用户表“Alex”的城市为“2”。在城市表中:id "2" 和 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".
所以问题是 - 如何使 SELECTED 只有一个标签 OPTION,其 VALUE 等于该城市的编号,该编号存储在“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
请帮忙
回答by Mayank Pandeyz
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
回答by Saumya Rastogi
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:
您不应该在视图中使用模型查询,因为这是一种不好的做法,因为您没有遵循 MVC 模式。你可以试试这个:
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:
在您的刀片视图中,您可以ternary operator
像这样使用:
@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!
希望这可以帮助!
回答by Soniya Reddy Basireddy
<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