public firstOrCreate ( array $attributes, array $values = [] ) : |
||
$attributes | array | |
$values | array | |
return |
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;
}
/** * 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; }