1

Hi I'm trying to use Route Model Binding using Route::resource(..) but when I try to return the model it is return as empty array []. Here is my route :

Route::resource('application','ApplicationController');

and from the view index.blade.php I successfully show all the data and linked them to to edit.blade.php like this :

@foreach ($appVersions as $appVersion)
                      <tr>
                        {{-- Type AppVersion --}}
                        <td>{{ $appVersion->type }}</td>

                        {{-- Message Alert --}}
                        <td>{{ $appVersion->alert }}</td>
                        {{-- Version --}}
                        <td>{{ $appVersion->version }}</td>
                        {{-- Link --}}
                        <td>{{ $appVersion->link }}</td>
                        {{-- Plaform --}}
                        <td>{{ $appVersion->platform }}</td>
                        {{-- Action --}}
                        <td><a class="glyphicon glyphicon-pencil cursor-hand" href="{{ route('application.edit',$appVersion) }}"></a></td>
                      </tr>
@endforeach

So I try to test to pass the model to edit page as you can see above route('application.edit',$appVersion) and try to directly return the model from the controller, this is my ApplicationController :

public function index()
    { //index fucntion to shows all the data appVersion
      try{
        // $data['appVersion'] = AppVersion::all();
        $appVersion = AppVersion::find(1);
        return view('application.index', compact('appVersion'));
      }catch(\Illuminate\Database\QueryException $e){
        return response()
                ->json([
                    'status'=>'failed',
                    'status_code'=>500,
                    'message' => 'An unexpected error has occurred on the server. Please contact server administrator.'
                  ], 500);
      }

    }

public function edit(AppVersion $appVersion)
    {//edit function to showing edit page
      // if I return $appVersion here it will return [].
      $type = ['Major'=>2, 'Minor'=>1];
      return view('application.edit',compact('appVersion','type'));
    }

so I've read the documentation about Route Model Binding but it is only showing the sample using the specific route like GET.

I've seen and checked neontsunami project it uses Route::resource(...) for Route Model Binding. I'm kinda lost here, and try to figure and where did I miss ?

NOTE : I'm using laravel 5.2

2 Answers 2

2

You need to use the same type-hinted variable name. In your case $application.

I recommend the following updates to your code:

routes.php

Route::resource('applications','ApplicationController');

ApplicationController.php

public function edit(AppVersion $application)
    {
      // if I return $appVersion here it will return [].
      $type = ['Major'=>2, 'Minor'=>1];
      return view('application.edit',compact('application','type'));
    }
Sign up to request clarification or add additional context in comments.

3 Comments

this is works, but since the docs do not mention this any another approach?
It's mentioned in the documents, that you have to type hint your variables.
oh yeah you're right, its my bad doesnt fully understand them
0

You're saying the docs only mention a GET route, but that seems to be exactly what you need. The route you are calling in your Blade view is this:

GET /application/{application}/edit

Now the only parameter it expects is application; you are passing it $appVersion. So in your Controller method you can access your route parameter, as shown above. This works out-of-the-box with Laravel if you typehint a Model in your Controller method. However, if you want to bind a parameter that's not an id to a Model instance you should do so in your RouteServiceProvider, like this:

public function boot(Router $router)
{
    parent::boot($router);

    // route bindings
    $router->bind('application', function ($appVersion) {
        return Application::where('version', $appVersion)->first();
    });
}

Comments

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.