Laravel / blade如何将所选值插入数据库

时间:2019-12-16 19:47:42

标签: html laravel bootstrap-4 laravel-blade

我正在尝试将选择字段中的数据存储到数据库中。

我已经尝试过了:

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

它从数据库显示并将新输入存储到数据库-一切正常。

然后我有这个我需要的东西:

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

代码的第二部分在选择字段中显示来自databse的数据,但是当我将其放入表单而不是先前的代码部分时不会更新。还尝试添加name="type"

我将不胜感激。

更新

我的后端:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

 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();
    }

插入请求:

public function rules()
    {
        return [

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

        ];

2 个答案:

答案 0 :(得分:0)

对于多项选择,您需要尝试name="type[]"

php端,您应该将type变量设为array

让我知道是否有帮助。

答案 1 :(得分:0)

PUT,删除功能可用于欺骗

<form>标签中使用方法发布

<form method="POST">

并在末尾手动添加

<input type="hidden" name="_method" value="PUT">