Error when passing data from a multiple select field to the BD LARAVEL

1

Well I get the data of a table from my BD and I send them to a select. When I want to save the data of the select to the database I get the error that the field matters has no value

But when checking the data that is sent by post it turns out that if it was sent

Here are my codes

view

<label class="my-1 mr-2" for="inlineFormCustomSelectPref">
    Materia 
      <select class="custom-select " id="inlineFormCustomSelectPref " id="materias" name="materias" >

            <option  selected >Elige...</option>
            @foreach ($materias as $id => $name)
                <option value="{{ $name }}">{{$name}}
                {!! $errors->first('name', '<span class=error>:message</span>') !!}
                </option>
            @endforeach
      </select>
</label>

controller

public function create()
{
    //
    $materias = Materia::pluck('nombre','id');
    return view('libros.create',compact('materias'));

}

/**
 * Store a newly created resource in storage.
 *
 * @param  App\Http\Requests\CreateLibroRequest;  $request
 * @return \Illuminate\Http\Response
 */
public function store(CreateLibroRequest $request)
{
    //
    //dd($request->all());
     $libro = Libro::create( $request->all() );

     //$libro->materias()->attach($request->materias);
    return redirect()->route('libros.index');
}
    
asked by Miguel Olea 11.07.2018 в 06:00
source

1 answer

1

Look at the following example.

<select name="channel_id" id="channel_id" class="form-control" required>
  <option value="">Choose a option...</option>
  @foreach($channels as $channel)
    <option value="{{ $channel->id }}">{{ $channel->name }}</option> 
  @endforeach
</select>
    
answered by 11.07.2018 / 06:38
source