Laravel 5.6 将日期时间保存到数据库

Laravel 5.6 save datetime to database

我对使用数据库 mysql 在 Laravel 中保存输入日期和时间有疑问。我尝试使用 dateTime 类型将数据保存到我的数据库中,我在数据库中的字段。我将 datetimepicker 用于 Bootstrap 4,其输入格式如 ex: 10/30/2018 12:00 PM。

错误说数据丢失

我的VoucherController

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Voucher;
use App\Image;
use App\Category;
use Carbon\Carbon;
use App\Merchant;
use Validator;
class AdminVoucherController extends Controller
{
public function __construct()
{
    $this->middleware('auth:admin');
}

public function indexVouchers()
  {
      $vouchers = Voucher::all();
      return view('Admin.Vouchers.Index')->with('Vouchers', $vouchers);
  }




  public function showFormVouchers()
  {
      $category = Category::all();
      $merchant = Merchant::all();
      return view('Admin.Vouchers.Form')->with(['categories' => $category,
      'merchants' => $merchant
      ]);
  }



    public function storeVouchers(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'photos' => 'required',
            'photos.*' => 'image|mimes:jpeg,png,jpg|max:2048',
            'price_normal' => 'required|integer',
            'price_discount' => 'required|integer',
            'amount' => 'required|integer',
            'description' => 'required'

    ]);


    $date = str_replace('.', '-', $request->input('startFrom'));
    // create the mysql date format
    $dateformat= Carbon::createFromFormat('d-m-Y H:i:s', $date);


    $date1 = str_replace('.', '-', $request->input('expiredUntil'));
    // create the mysql date format
    $dateformat1= Carbon::createFromFormat('d-m-Y H:i:s', $date1);

    $voucher = new Voucher();
    $voucher->name = $request->name;
    $voucher->description = $request->description;
    $voucher->start_from = $dateformat;
    $voucher->expired_on = $dateformat1;
    $voucher->value = $request->amount;
    $voucher->merchant_id = $request->merchant;
    $voucher->categories_id = $request->category;
    $voucher->save();





     if($request->hasfile('photos[]'))
     {

        foreach($request->file('photos[]') as $image)
        {
            $name= $this->getFileName($request->photos);
            $image = new Image();
            $image->name = $name;
            $image->move(public_path().'/img/'   .$request->input('merchant') , $name);
            $image->voucher_id = $voucher->id;
            $image->save();
        }
     }

     return response()->json([

        'redirect_url' => route('create.vouchers')
      ]);

    }


    private function getFileName($file)
    {
        $dateNow = Carbon::now();
        $namefile = $dateNow->toDateString();
        return $namefile. '-' .$request->merchant. '.'.$file->extension();
    }
 }

Voucher.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\ordered;

class Voucher extends Model
{

protected $fillable = ['name','start_from','expired_on','price_normal','price_discount','status'];

protected $table = 'vouchers';

public function details()
{
    return $this->hasMany(ordered::class);
}

public function categories()
{
    return $this->belongsTo(Category::class);
}

public function setStartFromAttribute($value)
{
    $this->attributes['start_from'] = Carbon\Carbon::createFromFormat('d-m-Y H:i',$value);
}

public function setExpiredOnAttribute($value)
{
    $this->attributes['expired_on'] = Carbon\Carbon::createFromFormat('d-m-Y H:i',$value);
}
}

在Laravel中,需要传递格式化日期:

public function setStartFromAttribute($value)
{
    $this->attributes['start_from'] = $value;
}

$voucher->start_from = $dateformat->format('Y-m-d H:i:s');

首先,您应该弄清楚日期是如何发布到您的控制器的。

dd($request->startFrom);

这样您就可以确保它确实被发布了。

其次,您使用 Carbon 的方式有点冗长。你可以简单地做这样的事情。

$date = Carbon::parse($request->startFrom)->format('d-m-Y H:i:s');

这会将您的日期格式化为适合您数据库的正确格式。

在没有实际看到错误的情况下,我只能猜测这是原因,因此是您需要的答案。