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
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 yourschema::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!
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.