Skip to content
Advertisement

Insert a value at a NOT NULL column int in SQL Laravel Eloquent

$add_v = new AppModelsHmsBbrCategory;
$add_v->id->max(id)+1;
$add_v->category_id->max(category_id)+1;
$add_v->category_name='Insert 1';
$add_v->category_description='Insert Desc';
$add_v->created_by='Me';
$add_v->created_datetime=CURRENT_TIMESTAMP;
$add_v->save();

1

I need to have this eloquent inserting data into my table with my first two columns, id, category_id be added with auto increment, specifically, the highest value + 1.

my code above has the error:

SymfonyComponentDebugExceptionFatalThrowableError
Call to a member function max() on null

what can I at the “max()” for this to function as intended? any help would be appreciated thanks.

Advertisement

Answer

you have error in

$add_v->category_id->max(category_id)+1;

I think you need

$category=AppModelsHmsBbrCategory::orderByDesc('id')->first();

then

 $add_v->category_id=$category->id+1;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement