How does laravelEloquentcreate method know whether the data is successfully updated?

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.