Is there a way to use firstOrCreate with an interval of dates. I know you can use:
Model::firstOrCreate(['field' => $value], ['created_at' => $dateValue]);
But is it possible to check if the record exist between the last few days?
Is there a way to use firstOrCreate with an interval of dates. I know you can use:
Model::firstOrCreate(['field' => $value], ['created_at' => $dateValue]);
But is it possible to check if the record exist between the last few days?
If you look at the source for firstOrCreate
, all it does to check the conditions is pass it to where()
:
if (! is_null($instance = $this->where($attributes)->first())) {
return $instance;
}
Meaning you could use anything that could be passed to where
, which includes an array of arguments:
Model::firstOrCreate([
['date', '>', $start],
['date', '<', $end]
], ['created_at' => $dateValue]);