Illuminate\Database\Eloquent\Builder::updateOrCreate PHP Method

updateOrCreate() public method

Create or update a record matching the attributes, and fill it with values.
public updateOrCreate ( array $attributes, array $values = [] ) : Model
$attributes array
$values array
return Model
    public function updateOrCreate(array $attributes, array $values = [])
    {
        $instance = $this->firstOrNew($attributes);
        $instance->fill($values)->save();
        return $instance;
    }

Usage Example

示例#1
0
 /**
  * Store settings in the database.
  */
 public function save()
 {
     if ($this->unsaved) {
         $all = $this->getData();
         $data = array_dot($all);
         foreach ($data as $key => $value) {
             $this->options->updateOrCreate(compact('key'), compact('key', 'value'));
         }
         $this->options->whereNotIn('key', array_keys($data))->delete();
         $this->unsaved = false;
     }
 }
All Usage Examples Of Illuminate\Database\Eloquent\Builder::updateOrCreate