Search code examples
laravelcountcontrollerinsert

laravel controller how to insert an id without auto increment?


        $category = new HmsBbrCategory;
        $category->category_name = $request->input('category_name');
        $category->category_description = $request->input('category_description');
        $category->save();
        
        $category->category_id = $category->id;
        $category->save();

I have this table, only the id is set to not null. I need to insert the column WITHOUT using auto increment, what do I add to my function?

1

I was thinking to count the id and just to add +1 so that the numbers are in squence:

$category->id = id()->count()+1;

This line is not counting though


Solution

  • fetch the last id from db

    $categoryId=HmsBbrCategory::orderByDesc('id')->first();
    
    $autoIncId=$categoryId->id+1;
    
    $category = new HmsBbrCategory;
    $category->category_name = $request->input('category_name');
    $category->category_description = $request->input('category_description');
    $category->id=$autoIncId;
    $category->category_id =$autoIncId;
    $category->save();