繁体   English   中英

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

[英]Route Model Binding using resource return empty array

嗨,我正在尝试使用Route::resource(..)使用路由模型绑定,但是当我尝试返回模型时,它以空数组[]形式返回。 这是我的路线:

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

从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

所以我尝试测试将模型传递到编辑页面,如上面的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'));
    }

因此,我已经阅读了有关路由模型绑定的文档,但仅显示了使用诸如GET之类的特定路由的示例。

我已经查看并检查了neontsunami项目,项目使用Route::resource(...)进行路由模型绑定。 我有点在这里迷路了,想弄清楚我想念哪里?

注意 :我正在使用laravel 5.2

您需要使用相同的类型提示变量名。 在您的情况下$application

我建议对代码进行以下更新:

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

您说的是文档仅提及GET路线,但这似乎正是您所需要的。 您在“刀片”视图中呼叫的路由是这样的:

GET /application/{application}/edit

现在它期望的唯一参数是application 您正在传递它$appVersion 因此,在Controller方法中,您可以访问route参数,如上所示。 如果您在Controller方法中键入模型提示,这将与Laravel开箱即用。 但是,如果要将非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