laravel5.6将datetime保存到数据库

dgenwo3n  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(455)

我有一个关于使用数据库mysql在laravel中保存输入日期和时间的问题。我尝试将数据保存到我的数据库中,该数据库中的字段使用datetime类型。我使用datetimepicker作为bootstrap 4的输入格式,例如ex:10/30/2018 12:00 pm。
错误显示数据丢失
我的担保人

<?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();
    }
 }

凭证.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);
}
}
xsuvu9jc

xsuvu9jc1#

首先,你应该计算出确切的日期是如何被张贴到您的控制器。

dd($request->startFrom);

这样你就可以确保它确实被发布了。
其次,他们使用碳的方式有点冗长。简单地说,你可以这样做。

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

这将格式您的日期为您的正确格式。
没有真正看到错误,我只能猜测这是原因,因此答案你需要。

cu6pst1q

cu6pst1q2#

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

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

或者

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

相关问题