Laravel 5.6 save datetime to database

Viewed 16085

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.

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

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 Answers

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.

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

This will format your date to the correct format for you db.

Without actually seeing the error I can only guess at this being the reason and therefore the answer you need.

In Laravel, you need to pass the formated date :

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

Or

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