Ad

Laravel/blade How To Insert Selected Value To Database

- 1 answer

I am trying to store data from a select field to database.

I have tried this:

<div class="form-group">
     <label>Type</label>
     <input style="width:10%" type="text" name="type" class="form-control" value="{{$findBook->type}}">
</div>

It displays from database and stores a new input to database - all good.

Then I have this what i need:

<select class="selectpicker" multiple>
  @foreach($books as $book)
 <option value="{{ $book->type }}" {{ ( $book->type == $findBook->type) ? 'selected' : '' }}>{{$book->type}}</option>
  @endforeach
</select>

This second part of the code displays data from databse in a select field, but does not update when I put it in a form instead of previous code part. Also tried to add name="type" .

I would appreciate any advice.

Update

My back end:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

    class Book extends Model
    {
        protected $table = 'books';
        protected $fillable = ['title', 'type', 'description'];
    }

and

 public function store(Request $request)
    {

       $emps = new Book;
       $emps->title = $request->input('title');
       $emps->type = $request->input('type');
       $emps->description = $request->input('description');
$emps->save();
    }

Insert request:

public function rules()
    {
        return [

'title' => 'required',
'type' => 'required',
'description' => 'required',

        ];
Ad

Answer

for multiple select, you need to try name="type[]"

And on your php side, you should have type variable as array.

let me know if that helps.

Ad
source: stackoverflow.com
Ad