21

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
jamis0n
  • 3,610
  • 8
  • 34
  • 50

3 Answers3

28

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.

markvaneijk
  • 486
  • 4
  • 3
  • If this works its exactly what I'm looking for. Is this documented anywhere in the Laravel docs? I looked through them pretty thoroughly. Will this return a boolean? – jamis0n Dec 03 '12 at 15:25
  • An Eloquent model can be adjusted by it's properties. But also by using Laravel's native database model named Fluent. It's mentoined in the docs here: http://laravel.com/docs/database/fluent#update – markvaneijk Dec 04 '12 at 07:48
  • Turns out `$updateOrder = Order::update($thisOrderID, $thisOrder);` works just fine. I had an error in my validation, which is why I was getting unexpected return results. – jamis0n Dec 04 '12 at 14:16
  • Doesn't the second way double the query? – Ulugov Dec 01 '22 at 06:22
3

What do you think of this:

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

    $order = Order::find( $thisOrderID );

    //CHECK FOR NEW OR EXISTING ORDER
    if( $order ) {
        $order->title = 'New order title';
        $order->desc = 'New description';
        $order->save();
    }
}
  • This works great until you have several fields to update, which then makes this seem tedious and redundant as theyre already defined in the array. I'm hoping @MarkVanEijk has the right answer above. – jamis0n Dec 03 '12 at 15:27
2

erm , I will suggest to do it this way, if $thisOrder include the key, it will just update the record, else it will just create a new record.

$thisOrder = array(
     'orderNo' => Input::get('orderNo'),
     'qty' => Input::get('quantity'),
     'desc' => Input::get('description'),
);

$updateOrder = Order::save($thisOrder);

if Input::get('orderNo') no value will be create else update

Knight
  • 484
  • 4
  • 10