Laravel Update Syntax - Updating Record with Array

jamis0n picture jamis0n · Dec 3, 2012 · Viewed 36.8k times · Source

I'm having trouble doing a basic record update via POST in Laravel.

I have captured all the post data in an array, and if the existing Order# is 0, then I create a new record (works fine). Otherwise I update the existing record.

Order.php

class Order extends Eloquent {
    public static $table = 'my_orders';
}

Routes.php

//Handle a new order POST
Route::post('order', array('do' => function() {
    $thisOrder = array(
         'qty' => Input::get('quantity'),
         'desc' => Input::get('description'),
    );

    $thisOrderID = Input::get('orderNo');

    //CHECK FOR NEW OR EXISTING ORDER
    if($thisOrderID > 0) {
        //THIS FUNCTION SOMEHOW RETURNS THE FUNCTION CALL AND DOESNT CONTINUE PAST
        //AND THE RECORD IS NOT UPDATED
        $updateOrder = Order::update($thisOrderID, $thisOrder);
    }
}

Update: The code above does in fact work. I had a validation error, which was causing the function to return early.

Answer

markvaneijk picture markvaneijk · Dec 3, 2012

Instead of this line:

$updateOrder = Order::update($thisOrderID, $thisOrder);

You need to do:

$updateOrder = Order::find($thisOrderID)->update($thisOrder);

With find() (which equals where_id()) you select a specific row from the database and with update you pass the new data.