public function nextid()
{
// ref is the counter - change it to whatever you want to increment
$this->ref = self::getID();
}
public static function bootUseAutoIncrementID()
{
static::creating(function ($model) {
$model->sequencial_id = self::getID($model->getTable());
});
}
public function getCasts()
{
return $this->casts;
}
private static function getID()
{
$seq = DB::connection('mongodb')->getCollection('counters')->findOneAndUpdate(
['ref' => 'ref'],
['$inc' => ['seq' => 1]],
['new' => true, 'upsert' => true, 'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER]
);
return $seq->seq;
}
$car = new Car();
$car->nextid(); // auto-increment
$car->name = "Ferrari",
$car->save();
Thanks, It's Works