将日期选择器值保存到 Laravel 中的数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41748577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:10:05  来源:igfitidea点击:

Saving a datepicker value to database in laravel

laravellaravel-5.3

提问by Sir George

I have this input in my form... I need to store the date picked from it, but I get a null value when I submit

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

<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">

am I missing something?? when I submit I don't get any values in the database, I am using laravel5

我错过了什么吗??当我提交时,我在数据库中没有得到任何值,我正在使用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');
   }

回答by Alexey Mezenin

Parsing dates should work:

解析日期应该有效:

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

Also, it's a good idea to put startand endto the $datesarray.

而且,这是一个好主意,把startend$dates阵列。

回答by Jaymin Panchal

You need to convert your date to the format the DB accept like Y-m-di.e. 2017-01-19.

您需要将日期转换为DB接受类似的格式Y-m-d,即2017-01-19

Try to convert it before storing in DB

在存储到 DB 之前尝试转换它

like,

喜欢,

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

回答by Jaymin Panchal

Use strtotime() on your first date then date('Y-m-d') to convert it back:

在第一次约会时使用 strtotime() 然后使用 date('Ym-d') 将其转换回来:

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

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

echo $newformat;

// 2003-10-16

// 2003-10-16

Converting string to Date and DateTime

将字符串转换为日期和日期时间