表单选择存储ID而非NAME

时间:2019-08-24 04:04:54

标签: mysql laravel-5 select model

我正在尝试存储从mysql中检索到的所选值(名称),但它存储的是id而不是name?有什么建议吗?

观看次数

{{ Form::select('dept',$departments->pluck('name'),null, ['class' => 'form-control','required','placeholder'=>'Select Department']) }}

路线

Route::get('/', function () {
    $departments=Department::pluck('name','id');
    return view('index',compact('departments'));
});

控制器

public function store(Request $request)
    {
        $this->validate(request(), [
            'fname' => 'required',
            'lname' => 'required',
            'staffId' => 'required',
            'email' => 'required|email',
            'dept' => 'required',
            'role' => 'required',
            'password' => 'required|confirmed'
        ]);
        $departments=Department::pluck('name','id');
        $departments->name = $request ->input('dept');
        $user = User::create(request(['fname','lname','staffId','email','dept','role','password']));

        auth()->login($user);

        return redirect()->to('/home2');
    }

型号

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Department extends Model
{
    protected $fillable=['name'];
    protected $table='department';

}

1 个答案:

答案 0 :(得分:0)

解决了问题

<select class="form-control" id="dept" name='dept'>
@if($departments->count() > 0)
@foreach ($departments as $department)
<option value="{{ $department->id }}">{{$department->name}}</option>
@endforeach
@else
No Record Found
@endif  
</select>

下拉 https://ibb.co/bvR6rc4

MySQL https://ibb.co/5FG2gWW