在laravel中将datepicker值保存到数据库

时间:2017-01-19 17:57:50

标签: laravel laravel-5.3

我的表单中有这个输入...我需要存储从中选择的日期,但是当我提交时我得到一个空值

<script type="text/javascript">
   $(function() {
       $( "#datepicker" ).datepicker();
   });
</script>

<input type="text" required="" placeholder="When are You Coming Back" name="datepicker2"  id="datepicker2" value="" name="datepicker2" class="txt">
我错过了什么吗?当我提交时,我没有在数据库中获得任何值,我正在使用laravel5

class LeaveController extends Controller
{
     public function ApplyLeave(Request $request)
    {

        Auth::user()->sent()->create([
            'tel'       => $request->tel,
            'email'    => $request->email,
            'start' => $request->datepicker,
            'end'       => $request->datepicker1,
            'supervisor'    => $request->supervisor,
            'department' => $request->department,
            'name'    => $request->name,
            'adress' => $request->adress,
        ]);   
        return view('home');
   }

3 个答案:

答案 0 :(得分:3)

解析日期应该有效:

'start' => Carbon::parse($request->datepicker),
'end' => Carbon::parse($request->datepicker1),

此外,最好将startend放入$dates数组。

答案 1 :(得分:1)

您需要将日期转换为数据库接受的格式,例如Y-m-d,即2017-01-19

尝试在存储到DB

之前进行转换

喜欢,

public function ApplyLeave(Request $request){

    Auth::user()->sent()->create([
        'tel'       => $request->tel,
        'email'    => $request->email,
        'start' => date("Y-m-d", strtotime($request->datepicker)),
        'end'       => date("Y-m-d", strtotime($request->datepicker1)),
        'supervisor'    => $request->supervisor,
        'department' => $request->department,
        'name'    => $request->name,
        'adress' => $request->adress,
    ]);   
    return view('home');
}

答案 2 :(得分:1)

在第一个日期然后使用strtotime(),然后使用日期('Y-m-d')将其转换回来:

$time = strtotime('10/16/2003');

$newformat = date('Y-m-d',$time);

echo $newformat;

// 2003-10-16

Converting string to Date and DateTime