Eli
Eli

Reputation: 1276

How to save multiple request parameters in 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

Upvotes: 0

Views: 2298

Answers (3)

P0rt4rd
P0rt4rd

Reputation: 46

Considering your model name is PurchaseOrder, you first need to create a new instance of it. Then, use the save method.

public function store(Request $request)
{
    $purchaseOrder = new PurchaseOrder;
    $purchaseOrder->overall_total = $request['overall_total'];
    $purchaseOrder->purchase_orders = serialize($request['purchase_orders']);
    $purchaseOrder->save();
}

See https://laravel.com/docs/5.2/eloquent#basic-inserts for more info.

Upvotes: 2

stratedge
stratedge

Reputation: 2820

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

Upvotes: 2

Drudge Rajen
Drudge Rajen

Reputation: 7987

Something like this :

DB::table('purchaseOrder')->insert(
    ['purchase_orders' =>  $serialize_po,'overall_total' => $overall_total]
);

See doc if you want to explore more.

Upvotes: 1

Related Questions