Laravel controller insert two id columns at save()

Viewed 173

I am trying to add two columns: id and category_id and I need both to be the same value

        $category = new HmsBbrCategory;
        $category->id = increments();
        $category->category_id = id+1;
        $category->category_name = $request->input('category_name');
        $category->category_description = $request->input('category_description');
        $category->save();
        return response()->json([
            'status'=>200,
            'message'=>'Category Added!',
           
        ]);

1

as you can see, both columns return null, tried adding two lines but of course they do not work:

        $category->id = increments();
        $category->category_id = id+1;

both values have no form, I just need them auto incremented and the same. What should I change here to add a value in the table thats not taken from a input field?

UPDATE

As suggested I incremented the id but my console error keeps saying my category_id is null:

        $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();

console error:

Failing row contains (3, null, 4, 4).

is it because the save function tries to insert it at category_id that needs not be not null?

Update:

I set the category_id column as not required to be not null, but the columns are still null even when the save() and update is added as suggested.

$category->update(['category_id '=> $category->id]);

$category->category_id = $category->id;
$category->save();

2

MODEL:

  protected $fillable = [ "id", "category_id", "category_name", "category_description" ];

FULL CONTROLLER

class BBRCategoryConfigurationController extends Controller {
public function index(){
    return view('frontend.bbr-settings.bbr-category-configuration');
}

public function store(Request $request)
{
    $validator = Validator::make($request->all(), [
        'category_name'=>'required|max:191',
        'category_description'=>'required|max:191',
    ]);
    if($validator->fails())
    {
        return response()->json([
            'status'=>400,
            'errors'=>$validator->messages(),
        ]);
    }
    else {
        $category = new HmsBbrCategory;
        $category->category_name = $request->input('category_name');
        $category->category_description = $request->input('category_description');
        $category->save();
        $category->update(['category_id '=> $category->id]);
        
        return response()->json([
            'status'=>200,
            'message'=>'Category Added!',
           
        ]);
    }
    
}
1 Answers

Set id as auto increment in your database table then do the following

$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();

or For this make sure to fieldname is fillable

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