Skip to content

Commit 50032fa

Browse files
committed
First or create on builder
1 parent 0b810e7 commit 50032fa

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/Jenssegers/Mongodb/Schema/Builder.php

+17
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ public function create($collection, Closure $callback = null)
115115
}
116116
}
117117

118+
/**
119+
* Get the first record matching the attributes or create it.
120+
*
121+
* @param array $attributes
122+
* @return \Illuminate\Database\Eloquent\Model
123+
*/
124+
public function firstOrCreate(array $attributes)
125+
{
126+
if (! is_null($instance = $this->where($attributes)->first())) {
127+
return $instance;
128+
}
129+
130+
$instance = $this->create($attributes);
131+
132+
return $instance;
133+
}
134+
118135
/**
119136
* Drop a collection from the schema.
120137
*

0 commit comments

Comments
 (0)