Search code examples
phplaravelcrud

Laravel CRUD API - in update method if I send a Model I have only model with out ID or other information


Laravel CRUD API - in update method if I send a Model I have only model with out ID or other information My expamle is :

 public function update(GoogleIndexingUpdate $request, GoogleIndexing $page)
{
        dd($page);
}

GoogleIndexingUpdate is Request GoogleIndexing is Model dd($page) - return model, no current model But if I used with out model I have ID

 public function update(GoogleIndexingUpdate $request, $page)
{
        dd($page);
}

dd($page) is 1

Ho can I use Model in method query to have straightaway current model ?

Return current model


Solution

  • If you are successfully obtaining the ID after removing the Model Name from the function arguments, then this is related to your route binding.

    Double check your route, for example if your route is something like this - https://example.com/api/indexing/{page} then only in controller you can access the model directly, it's called Implicit Binding.

    In case you don't want to change the route, rather change the Binding Key, you can change the variable name on controller like this -

    public function update(GoogleIndexingUpdate $request, GoogleIndexing $yourroutevariable)
    {
        dd($yourroutevariable);
    }