I want to use the create method in Eloquent in laravel to insert data. By default, this method returns the model object. But how do I know whether the data is successfully inserted? The previous framework will return a bool type. The create method code is as follows: {code...} I want to use the create method in Eloquent in laravel to insert data. By default, this method returns the model object. But how do I know whether the data is successfully inserted? The previous framework 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 use the create method in Eloquent in laravel to insert data. By default, this method returns the model object. But how do I know whether the data is successfully inserted? The previous framework 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, if it fails, false is returned. If it succeeds, a model object is returned.
You can use another method.save()
For more information, see the following section.
$ Obj = new Order; $ obj-> status = 1; $ obj-> price = 100; $ res = $ obj-> save (); // returns true or falseif (! $ Res) {// Log: error ('failed', ['context' => $ arrData]); return 0;} return $ obj-> id; // return the ID of the new record