mercredi 24 février 2016

Laravel - Strange behaviour of Table::find($id), it returs array of rows, instead of 1 row

How I understand Table::find($id) should be equivalent to Table::where('id', $id)->first(), but I receive array instead of 1 record..

Order.php (model):

public function change_user($order_id, $user_id) {
    $order = Order::find($order_id);
    dd($order);
    if ($order == null) return false;
    $order->user = $user_id;
    return $order->save();
} 

and result of dd:

Collection {#270 ▼
  #items: array:1 [▼
    0 => Order {#271 ▼
      +timestamps: true
      #guarded: array:1 [▶]
      #hidden: array:2 [▶]
      #connection: null
      #table: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      #attributes: array:30 [▶]
      #original: array:30 [▶]
      #relations: []
      #visible: []
      #appends: []
      #fillable: []
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
  ]
}

If I am wrong that find should return 1 row, I still have an issue with this, because I receive error message, when I remove dd():

BadMethodCallException in Macroable.php line 81: Method save does not exist.

    in Macroable.php line 81
    at Collection->__call('save', array()) in Order.php line 64
    at Collection->save() in Order.php line 64
    at Order->change_user(array('57'), '18') in RegistersUsers.php line 69
    at AuthController->register(object(Request), object(Order))
    at call_user_func_array(array(object(AuthController), 'register'), array(object(Request), object(Order))) in Controller.php line 76



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire