Laravel 5.6 将日期时间保存到数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53053583/
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
Laravel 5.6 save datetime to database
提问by andri purnama
I have question about save input date and time in Laravel using database mysql. I try to save data to my database which my field on database using dateTime type. I use datetimepicker for Bootstrap 4 which the input format like ex: 10/30/2018 12:00 PM.
我有关于使用数据库 mysql 在 Laravel 中保存输入日期和时间的问题。我尝试使用 dateTime 类型将数据保存到我的数据库中我的数据库字段。我将 datetimepicker 用于 Bootstrap 4,其输入格式如:10/30/2018 12:00 PM。
the error said Data missing
错误说数据丢失
my 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
<?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);
}
}
回答by Colin Barstow
First of all you should work out exactly how the date is being posted to your controller.
首先,您应该弄清楚日期是如何发布到您的控制器的。
dd($request->startFrom);
This way you can make sure it is actually being posted.
这样您就可以确保它确实被发布了。
Secondly they way you are using Carbon is a little long winded. Simply you can do something like this.
其次,他们使用 Carbon 的方式有点啰嗦。简单地你可以做这样的事情。
$date = Carbon::parse($request->startFrom)->format('d-m-Y H:i:s');
This will format your date to the correct format for you db.
这会将您的日期格式化为适合您 db 的正确格式。
Without actually seeing the error I can only guess at this being the reason and therefore the answer you need.
没有真正看到错误,我只能猜测这是原因,因此是您需要的答案。
回答by Vincent Decaux
In Laravel, you need to pass the formated date :
在 Laravel 中,您需要传递格式化的日期:
public function setStartFromAttribute($value)
{
$this->attributes['start_from'] = $value;
}
Or
或者
$voucher->start_from = $dateformat->format('Y-m-d H:i:s');