简体   繁体   English

使用资源返回空数组的路由模型绑定

[英]Route Model Binding using resource return empty array

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 [] . 嗨,我正在尝试使用Route::resource(..)使用路由模型绑定,但是当我尝试返回模型时,它以空数组[]形式返回。 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 : 从index.blade.php的视图中,我成功显示了所有数据,并将它们链接到edit.blade.php,如下所示:

@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 : 所以我尝试测试将模型传递到编辑页面,如上面的route('application.edit',$appVersion)并尝试直接从控制器返回模型,这是我的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 . 因此,我已经阅读了有关路由模型绑定的文档,但仅显示了使用诸如GET之类的特定路由的示例。

I've seen and checked neontsunami project it uses Route::resource(...) for Route Model Binding. 我已经查看并检查了neontsunami项目,项目使用Route::resource(...)进行路由模型绑定。 I'm kinda lost here, and try to figure and where did I miss ? 我有点在这里迷路了,想弄清楚我想念哪里?

NOTE : I'm using laravel 5.2 注意 :我正在使用laravel 5.2

You need to use the same type-hinted variable name. 您需要使用相同的类型提示变量名。 In your case $application . 在您的情况下$application

I recommend the following updates to your code: 我建议对代码进行以下更新:

routes.php routes.php文件

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

ApplicationController.php 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'));
    }

You're saying the docs only mention a GET route, but that seems to be exactly what you need. 您说的是文档仅提及GET路线,但这似乎正是您所需要的。 The route you are calling in your Blade view is this: 您在“刀片”视图中呼叫的路由是这样的:

GET /application/{application}/edit

Now the only parameter it expects is application ; 现在它期望的唯一参数是application you are passing it $appVersion . 您正在传递它$appVersion So in your Controller method you can access your route parameter, as shown above. 因此,在Controller方法中,您可以访问route参数,如上所示。 This works out-of-the-box with Laravel if you typehint a Model in your Controller method. 如果您在Controller方法中键入模型提示,这将与Laravel开箱即用。 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: 但是,如果要将非id的参数绑定到Model实例,则应在RouteServiceProvider中这样做,如下所示:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM