denizli
denizli

Reputation: 91

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.

Upvotes: 9

Views: 58428

Answers (2)

chantez
chantez

Reputation: 161

you used multiple select 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.

Upvotes: 5

Kaviranga
Kaviranga

Reputation: 666

If you are using a model define it in the controller and in the Routes.php file.

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.

Category.php (this is the model in app directory)

  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

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

  <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!

Upvotes: 0

Related Questions