With Laravel & Eloquent, I’d like to :
- Create a model, unless it already exists into the database (like
Model::firstOrCreateallows) - At the same time, pass it a few attributes, including some which are not listed in the
$fillablearray of the model (likeModel::forceCreateallows).
But how do I combine the capacities of firstOrCreate and forceCreate? Apparently, Model::forceFirstOrCreate doesn’t exist!
Advertisement
Answer
Use the unguarded method:
$model = Model::unguarded(function() {
return Model::firstOrCreate($attributes);
});