0

I am Using Resource controller in Laravel 5.6. I am following this tutorial. I found here Resource controller uses the Route model binding, This means that you dont need to fetch the specified task by the id. Laravel will do it for you. $task variable which is passed into the show() method is passed to the view via compact method.

In my code I am using below code in Controller.

 /**
     * Display the specified resource.
     *
     * @param  \App\sura  $sura
     * @return \Illuminate\Http\Response
     */
    public function show(Sura $sura)
    {
        return $sura;
    }

Here I am getting the Whole Sura object not the id.

Why I am getting the whole object not the id ? Where is the issue ?

3
  • can you share the resource you create Commented Jun 24, 2018 at 7:50
  • 1
    laravel.com/docs/5.6/routing#route-model-binding Commented Jun 24, 2018 at 7:51
  • @ali, Here is the route Route::apiResource('suras', 'SuraController'); . Thanks. Commented Jun 24, 2018 at 9:33

1 Answer 1

3

https://laravel.com/docs/5.6/routing#route-model-binding When dependency inject model

public function show(Sura $sura)
{
    return $sura; // it is instance of Sura 
}

For get id use this

public function show($suraId)
{
    dd($suraId);// return integer number
}
Sign up to request clarification or add additional context in comments.

2 Comments

@abuabu are you try it?
Thanks @Davit. I am reading your answer. But I would like to know about what is saying in the Tutorial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.