简体   繁体   English

使用Form:select()门面填充带有ID​​和名称的Laravel 5.2 Select框

[英]Populate Laravel 5.2 Select box with ID and Name wihout using Form:select() facade

I know there is already a question on stackoverflow about populating select box , but there are answers given using form:select() facade 我知道在stackoverflow上已经存在有关填充选择框的问题,但是使用form:select()门面给出了答案

I want to populate select box but not using form facade. 我想填充选择框,但不使用表单外观。 I did this and it worked for me 我做到了,对我有用

In controller 在控制器中

public function create()
{
    $states=General_states::all('state_name','state_id');
    return view('admin.addcity',compact('states'));
}

In view 视野中

 <select class="form-control" name="movie_language">
        @foreach($states as $data)
        <option value="{{ $data->state_id }}">{{ $data->state_name }} </option>
        @endforeach
</select>

But I want to use ::lists here like this 但我想在这里使用:: lists

In Controller 在控制器中

public function create()
{
    $states=General_states::lists('state_name','state_id');
    return view('admin.addcity',compact('states'));
}

But In view I am getting error message as trying to get the property of Non Object. 但是,鉴于我尝试获取非对象的属性,我收到了错误消息。 Help me so that I can use lists in controller and populate select box in view without using {{form::select() }} 帮助我,以便我可以使用控制器中的列表并在视图中填充选择框,而无需使用{{form :: select()}}

if you don't need filtering here is a short hand way.suppose your General_states namespace is App\\ 如果您不需要过滤,这是一种简便方法。假设您的General_states命名空间为App\\

{!! Form::select('movie_language', App\General_states::lists('state_name','state_id'),'', ['class'=>'form-control']) !!}

or if you want to another way just send me the dump of after changing your code like this and executing. 或者,如果您想使用另一种方法,只需在更改代码并执行后向我发送的转储。

public function create()
{
    $states=General_states::lists('state_name','state_id');
    dd($states);
    return view('admin.addcity',compact('states'));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM