Answer the question
In order to leave comments, you need to log in
How to get the max id and add the element with id+1 before it's taken?
I use two similar models and they should not have the same value in the id field. That is, the uniqueness within the two models must be respected. I solved this problem using the function:
static function getNextId() {
$mpMaxId = MasterProduct::max('id');
$gpMaxId = GroupedProduct::max('id');
if ( $mpMaxId > $gpMaxId ) {
return $mpMaxId+1;
} else {
return $gpMaxId+1;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question