Creating and Update Laravel Eloquent

后端 未结 13 1485
广开言路
广开言路 2020-11-27 11:59

What\'s the shorthand for inserting a new record or updating if it exists?



        
13条回答
  •  星月不相逢
    2020-11-27 12:42

    Save function:

    $shopOwner->save()
    

    already do what you want...

    Laravel code:

        // If the model already exists in the database we can just update our record
        // that is already in this database using the current IDs in this "where"
        // clause to only update this model. Otherwise, we'll just insert them.
        if ($this->exists)
        {
            $saved = $this->performUpdate($query);
        }
    
        // If the model is brand new, we'll insert it into our database and set the
        // ID attribute on the model to the value of the newly inserted row's ID
        // which is typically an auto-increment value managed by the database.
        else
        {
            $saved = $this->performInsert($query);
        }
    

提交回复
热议问题