php - ErrorException strtotime() expects parameter 1 to be string, array given
Can anyone help me? this is my controller, when i try to store 'purchase' data it show this message strtotime() expects parameter 1 to be string, array given
The error was on the $purchase->date = date('Y-m-d', strtotime($request->date));
and $purchaseDetail->date = date('Y-m-d', strtotime($request->date));
public function store(Request $request){
if($request->category_id == null){
return redirect()->back()->with('error', 'Please Purchase The Product');
} else{
// Multipale Data Insert start //
$purchase = new purchase();
$purchase->purchase_no = $request->purchase_no;
$purchase->date = date('Y-m-d', strtotime($request->date));
$purchase->description = $request->description;
$purchase->status = '0';
$purchase->created_by = Auth::user()->id;
DB::transaction(function() use($request,$purchase) {
if($purchase->save()) {
// Purchase Details Insert Start //
$category_id = count($request->category_id);
for ($i=0; $i < $category_id; $i++) {
$purchaseDetail = new purchaseDetail();
$purchaseDetail->date = date('Y-m-d', strtotime($request->date));
$purchaseDetail->purchase_id = $purchase->id;
$purchaseDetail->supplier_id = $request->supplier_id[$i];
$purchaseDetail->category_id = $request->category_id[$i];
$purchaseDetail->product_id = $request->product_id[$i];
$purchaseDetail->buying_qty = $request->buying_qty[$i];
$purchaseDetail->unit_price = $request->unit_price[$i];
$purchaseDetail->buying_price = $request->buying_price[$i];
$purchaseDetail->discount_amount = $request->discount_amount[$i];
$purchaseDetail->ppn = $request->ppn[$i];
$purchaseDetail->status = '0';
$purchaseDetail->save();
}
}
});
}
// Redirect
return redirect()->route('purchase.view')->with('success', 'Purchase Added Successfully');
}
Hello, i try to change $purchase->date = date('Y-m-d', strtotime($request->date));
into $purchase->date = $request->date;
But the result was TypeError
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in D:\Project Laravel\alc-pos\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 869
Answer
Solution:
As for my opnion you must not format your date when saving to database. Instead you can format it when outputit from database into your blade templates.
Anyway make a check on $request->date
, seems to be an array.
You can format your date using Laravel accessor in your model like this (example):
public function getFormattedStartDateAttribute()
{
return $this->start_date->format('d-m-Y');
}
and then call it in your blade template like this (example):
<input type="text" name="start_date" class="form-control" value="{{$trip->formatted_start_date}}">
More info about Laravel Mutators & Accessors : Laravel Docs
Also you can do another thing. On your schema::create
make:
$table->date('date_name_column');
and then just save it like follow:
$purchaseDetail->date = $request->input('date');
This way the date in your database will be save as follow : YYYY-MM-DD
Hope it helps!
Source