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

时间:2018-10-29 20:49:05

标签: php mysql laravel datetime laravel-5.6

我对使用MySQL数据库在Laravel中保存输入日期和时间有疑问。我尝试使用dateTime类型将数据保存到数据库中的字段。我为Bootstrap 4使用datetimepicker,其输入格式如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);
}
}

2 个答案:

答案 0 :(得分:2)

首先,您应该确切地确定日期如何发布到您的控制器。

dd($request->startFrom);

这样,您可以确保它实际上已被发布。

第二,您使用碳的方式有点长。只需执行以下操作即可。

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

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

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

答案 1 :(得分:0)

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

$: cat tst
#! /bin/env bash
declare -i lines=0 words=0 chars=0
case "$#" in
0)  wc ;;
*)  for file in $*
    do  read lines words chars x <<< "$( wc $file )"
        echo "$lines $words $chars $file"
    done ;;
esac
$: cat foo

hello
world
and
goodbye cruel world!

$: tst < foo
 6  6 40
$: tst foo tst
6 6 40 foo
9 38 206 tst

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