php 如何在 Laravel 中获取选择值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38677257/
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
How to get Select Value in Laravel
提问by denizli
Hello I want to try to get the value of the selected option but i always getting a null value when i dump out the variable.
您好,我想尝试获取所选选项的值,但是当我转储变量时,我总是得到一个空值。
My Select looks like this:
我的选择看起来像这样:
<div class="col-md-6">
<!--<input id="members" type="text" class="form-control" name="members">-->
<select name="members" class="js-example-basic-multiple js-states form-control" id="members" multiple="multiple" >
<!-- @foreach($users as $user)
<option value='{{$user->id}}'>{{$user->name}}</option>
@endforeach-->
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>
@if ($errors->has('members'))
<span class="help-block">
<strong>{{ $errors->first('members') }}</strong>
</span>
@endif
</div>
And my Controller function looks like this:
我的控制器功能如下所示:
$sprintname = $request->input('sprintname');
$startdate = $request->input('startdate');
$enddate = $request->input('enddate');
$members = $request->input('members');
dd($members);
I really dont know whats wrong and i hope someone can help me.
我真的不知道出了什么问题,我希望有人能帮助我。
The other inputs are fine but only with the select i get a null value.
其他输入很好,但只有选择我才能得到空值。
回答by chantez
you used multiple select try name="members[]"
您使用了多选尝试 name="members[]"
and in Controller
并在控制器中
public function Postdata(Request $request) {
$value = $request->members; }
ever you can do
dd(request->all());
for check data
`
你可以dd(request->all());
为检查数据做些什么
`
回答by kaviranga
If you are using a model define it in the controller and in the Routes.php file.
如果您使用模型,请在控制器和 Routes.php 文件中定义它。
Here what I have used in my sample project.I haven't use a controller for this.Got good results for laravel 5.0,5.1 and 5.2.
这是我在示例项目中使用的内容。我没有为此使用控制器。laravel 5.0、5.1 和 5.2 的效果很好。
Category.php (this is the model in app directory)
Category.php(这是app目录下的模型)
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Input;
class Category extends Model
{
protected $fillable = ['name'];
protected $table = 'categories';
public function category()
{
return $this->belongsTo('App\Category');
}
}
in Routes.php
在 Routes.php 中
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Category;
Route::get('/',function()
{
$categories = Category::all();
return view('category.index')->with('categories',$categories);
});
in the view
在视图中
index.blade.php
index.blade.php
<div class="form-group">
<label for="">Categories</label>
<select class="form-control input-sm" name="category" id="category">
@foreach($categories as $category)
<option value="{{$category->id}}">{{$category->name}} </option>
@endforeach
</select>
</div>
This work like a charm.Hope this information will be helpful for you!
这项工作就像一个魅力。希望这些信息对您有所帮助!