Laravel eloquent Create method inserts data, how do you know if the update was successful?

Source: Internet
Author: User
Keywords Php laravel
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
  • 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.