我如何理解 Table::find($id) 应该等同于 Table::where('id', $id)->first(),但我收到数组而不是 1 条记录。.
订货.php(型号):
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();
}
和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
}
]
}
如果我错了,查找应该返回 1 行,我仍然有这个问题,因为当我删除 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
如果你把一个数组传递给find
,它将返回一个集合。
似乎$order_id
是一个数组。
您是否更改了主键?
检查$id是否不是数组。如果是数组,Laravel返回超过1行。