简体   繁体   English

如何在 Laravel 中获取选择值

[英]How to get Select Value in Laravel

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.其他输入很好,但只有选择我才能得到空值。

you used multiple select try name="members[]"您使用了多个选择 try name="members[]"

and in Controller并在控制器中

public function Postdata(Request $request) {
$value = $request->members; }

Or you can do:或者你可以这样做:

dd( request()->all() );

To check all of the data.检查所有数据。

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!这项工作就像一个魅力。希望这些信息对您有所帮助!

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

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