Laravel - 获取选择菜单的值以输入数据库

时间:2017-04-25 10:54:33

标签: php html laravel

嗨所以我正在尝试更改我的表单以创建约会,我目前有一个foreach循环的单选按钮,它的工作,但当然它看起来不太好。所以我试图改为使用选择菜单让它看起来更好,但是当我尝试提交我的新表格时,它告诉我必须选择医生字段,所以它没有得到所选内容的实际值。

addappointmentform.blade(OLD VERSION如上所述,但这看起来很糟糕)

    <fieldset>
<legend>Select the Doctor</legend>
@foreach($doctors as $doctor)
    <div>
        <label for="dirBtn{{$doctor->id}}">
        <input id="dirBtn{{$doctor->id}}" type="radio" name="doctor" value="{{$doctor->id}}">Dr
        {{$doctor->surname}}
        </label>
    </div>
@endforeach
</fieldset>

addappointmentform.blade(NEW VERSION,这就是我想要的工作)

    <fieldset>
<select name="doctorsSelect">
    <option selected disabled>Please select a Doctor</option>
    @foreach($doctors as $doctor)


                <option value="{{ $doctor->id }}"
                name="doctor" input id="dirBtn{{$doctor->id}}" value="{{$doctor->id}}">
                Dr {{$doctor->surname}}</option>


    @endforeach
</select>
    </fieldset>

AppointmentController

function addAppointmentDatabase(Request $request)
    {
        $this->validate($request, [
            'time' => 'required|',
            'date' => 'required|min:10|max:10|filled',
            'doctor' => 'required',
            'user' => 'required',
        ]);
        //create a appointment
        $appointment = new Appointment();
        $appointment->time=$request->time;
        $appointment->date=$request->date;


        //assign doctor to appointment
        $doctor = Doctor::find($request->doctor);
        $appointment->doctor()->associate($doctor);

        $user = User::find($request->user);
        $appointment->user()->associate($user);

        //save the appointment
        $appointment->save();


        return redirect('all');
    }

2 个答案:

答案 0 :(得分:3)

您的选择名称是医生选择,将其更改为医生:

<select name="doctor">

答案 1 :(得分:3)

id您不需要option,而您的select也需要拥有之前name的{​​{1}}。

这样的事情:

input

希望这有帮助。