Search code examples
laraveleloquentlaravel-7type-hintinglaravel-models

Laravel type hinting, id return null on model save


I am trying to use type hinting for storing the value.

public function store(Model $model, ModelRequest $request) {
    $model->create($request->validated())->save();
    dd($model->id);
}

But, Id is returning null.


Solution

  • As said in comment by @aimme, you don't need save() methode. The save() methode return true or false.

    If you realy need to see what appened:

    $new_created= $model->create($request->validated());
    dd($new_created->id);
    

    if it not working then you have to check the return of $request->validated():

    dd($request->validated());