I want to insert the data using the Create method in eloquent in Laravel. This method returns the Model object by default, but how do you know if the inserted data is successful? The previous frame will return a bool type.
The Create method code is as follows:
public static function create(array $attributes = []) { $model = new static($attributes); $model->save(); return $model; }
Reply content:
I want to insert the data using the Create method in eloquent in Laravel. This method returns the Model object by default, but how do you know if the inserted data is successful? The previous frame will return a bool type.
The Create method code is as follows:
public static function create(array $attributes = []) { $model = new static($attributes); $model->save(); return $model; }
Finally found, if the failure is to return false, the successful word is to return a model object.
You can save()
use a different method, see below for specific usage
$obj = new Order;$obj->status = 1;$obj->price = 100;$res = $obj->save();//返回true or falseif(!$res){ //Log::error('failed', ['context' => $arrData]); return 0;}return $obj -> id;//返回新建记录的ID