dongliuxia9495 2016-03-20 16:42
浏览 83
已采纳

如何在laravel 5.2中保存多个请求参数?

I am just learning laravel now. And I have this problem. I have passed 2 request parameters to my controller function. First request parameter holds an object value, but I converted it to a serialized form since the field of my table where it will be saved has a text datatype. The second request parameter holds a overall_total calculated value and it has a float datatype field. My problem is, how would I store it in my database? I have tried to use the create function but it returns an error. Some forums regarding this are not so clear. I just can't figure it out yet. Can somebody help me with this? Here are my codes.

function store

public function store(Request $request){

       $serialize_po = serialize($request['purchase_orders']);
       $overall_total = $request['overall_total'];
       $purchase_orders_save = PurchaseOrder::create(?);

}

How would I save 2 parameters using create? or is there other way I can saved it?

Inside of $request['purchase_orders'] is shown in the image below

enter image description here

Inside of $request['overall_total'] is just a number. E.g. 310

My Database Table Structure is shown below

enter image description here

  • 写回答

3条回答 默认 最新

  • du27271 2016-03-20 17:00
    关注

    The create() function in Laravel accepts an associative array, where the array keys are the names of the columns, and the array values are the corresponding values for the columns.

    So your store function might look something like this:

    public function store(Request $request){
        $serialize_po = serialize($request['purchase_orders']);
        $overall_total = $request['overall_total'];
        $purchase_orders_save = PurchaseOrder::create([
            'purchase_orders' => $serialize_po,
            'overall_total' => $overall_total
        ]);
    }
    

    One other thing to note is that as a safety precaution, Laravel does not allow the properties of a model to be filled in this fashion out of the box. You will need to list in your model using the $fillable property which keys you will allow to be filled by passing in an associative array. If you don't, you'll likely get a MassAssignmentException.

    So in your model, you will likely need to have at least the following:

    class PurchaseOrder extends Model
    {
        protected $fillable = ['purchase_orders', 'overall_total'];
    }
    

    There are more options and ways to do this, but that is the basic and typical scenario.

    More info on using the create() method and handling mass assignment is available in the documentation: https://laravel.com/docs/5.2/eloquent#mass-assignment

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了