ErrorException strtotime()期望参数1为字符串,给定数组 [英] ErrorException strtotime() expects parameter 1 to be string, array given

查看:93
本文介绍了ErrorException strtotime()期望参数1为字符串,给定数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我吗?这是我的控制器,当我尝试存储购买"数据时,它显示此消息 strtotime()期望参数1为字符串,给定数组 错误发生在 $ purchase-> date = date('Ym-d',strtotime($ request-> date)); $ purchaseDetail-> date = date('Ym-d',strtotime($ request-> date));

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');
}

你好,我尝试将 $ purchase-> date = date('Ym-d',strtotime($ request-> date)); 更改为 $ purchase-> date = $ request-> date; 但是结果是 TypeError传递给Illuminate \ Database \ Grammar :: parameterize()的参数1必须为数组类型,给定字符串,在D:\ Project Laravel \ alc-pos \ vendor \ laravel \ framework \ src \ Illuminate \ Database \ Query \中调用第869行的Grammars \ Grammar.php

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.

无论如何都要检查 $ request-> date ,似乎是一个数组.

Anyway make a check on $request->date, seems to be an array.

您可以在模型中使用Laravel访问器设置日期格式(例如):

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}}">

有关Laravel Mutators&的更多信息访问者: Laravel文档

More info about Laravel Mutators & Accessors : Laravel Docs

您还可以做另一件事.在您的 schema :: create make:

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');

这样,数据库中的日期将被保存为:YYYY-MM-DD

This way the date in your database will be save as follow : YYYY-MM-DD

希望有帮助!

这篇关于ErrorException strtotime()期望参数1为字符串,给定数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆