Laravel:将日期提交到数据库 [英] Laravel: Submitting my dates to my database

查看:86
本文介绍了Laravel:将日期提交到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想将我从下拉日历中选择的日期提交给我的数据库.其他所有内容均已提交.我还将提供表的屏幕快照,因为我不确定我是否为日期使用了正确的数据类型,因此下面是CheckIn变量的片段,这是一个用于通过日期检查用户的变量:

I'm simply trying to submit my dates that are selected from a drop down calender to submit them to my database. Everything else is submitted. Also I will provide screenshots of my table as I'm unsure if I am using the correct data types for my dates so here is a snipping of the CheckIn variable which is one variable used to check the user in via date:

现在进入我的代码

型号:Cart.php

Model: Cart.php

    public $items = null;
    public $totalQty = 0;
    public $totalPrice = 0; 
    public $checkIn = null;
    public $checkOut = null;

    public function __Construct($oldCart)
    {
        if ($oldCart) {
            $this->items = $oldCart->items;
            $this->totalQty = $oldCart->totalQty;
            $this->totalPrice = $oldCart->totalPrice;
            $this->checkIn = $oldCart->checkIn;
            $this->checkOut = $oldCart->checkOut;
        }        
    }

PostsController.php:

PostsController.php:

    public function getCheckout()
     {
    if (!Session::has('cart')) {
        return view('shop.shopping-cart');
    }
    $oldCart = Session::get('cart');
    $cart = new Cart($oldCart);
    $total = $cart->totalPrice;
    return view('posts.checkout', ['total' => $total, 'checkIn' => $cart->checkIn, 'checkOut' => 
     $cart->checkOut,]);
     }

         public function postCheckout(Request $request)
        {
        ...
        $order = new Order();
        $order->cart = serialize($cart);
        $order->address = $request->input('address'); 
        $order->name = $request->input('name');
        $order->payment_id = $charge->id;
        $order->checkIn = $request->input('checkIn');
        $order->checkOut = $request->input('checkOut');
        Auth::user()->orders()->save($order);

当我在结帐时按Submit时,存储在数据库中的CheckIn和CheckOut值将显示为NULL.

When I press submit on my checkout, the values that are stored in my database for the CheckIn and CheckOut appear as NULL.

推荐答案

使用Carbon类并进行类似的操作

use the Carbon class and to something like this

课程文件上方

use Carbon\Carbon;

class  YouClassHereController
...
$order->checkIn = Carbon::parse($request->input('checkIn')); //it will create to  date class.

这篇关于Laravel:将日期提交到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆