firstOrCreate() public method

Get the first record matching the attributes or create it.
public firstOrCreate ( array $attributes, array $values = [] ) : Model
$attributes array
$values array
return Model
示例#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;
 }
示例#2
0
 /**
  * Get the first record matching the attributes or create it.
  *
  * @param array $attributes
  * @return \Illuminate\Database\Eloquent\Model 
  * @static 
  */
 public static function firstOrCreate($attributes)
 {
     return \Illuminate\Database\Eloquent\Builder::firstOrCreate($attributes);
 }