Illuminate\Database\Eloquent\Builder::firstOrCreate PHP Метод

firstOrCreate() публичный Метод

Get the first record matching the attributes or create it.
public firstOrCreate ( array $attributes, array $values = [] ) : Model
$attributes array
$values array
Результат Model
    public function firstOrCreate(array $attributes, array $values = [])
    {
        if (!is_null($instance = $this->where($attributes)->first())) {
            return $instance;
        }
        $instance = $this->model->newInstance($attributes + $values)->setConnection($this->query->getConnection()->getName());
        $instance->save();
        return $instance;
    }

Usage Example

Пример #1
0
 /**
  * Find an item by given values, or create it if it doesn't exist
  *
  * @param array $data
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function firstOrCreate(array $data)
 {
     $item = $this->builder->firstOrCreate($data);
     // Reset the query builder
     $this->newBuilder();
     return $item;
 }
All Usage Examples Of Illuminate\Database\Eloquent\Builder::firstOrCreate