Laravel 5.5 - 缺少[Route:]所需的参数

时间:2018-04-16 21:37:59

标签: laravel

我想要实现的目标非常简单,但我只是让它变得比它需要的更难。所以我正在寻求帮助显然,我缺乏Laravel经验。

我想要做的就是有一个可以通过文本输入更新数据库条目的表单。这必须是动态的,因为它被用于几个数据库,我不想为它们提供多个文件。

对于可能的凌乱/废话代码提前抱歉...

这是我的路线:

Route::get('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController@greeting');
Route::post('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController@updateGreeting')->name('greeting.update');

PlayerProfileController

  // Display greeting message
  public function greeting($server, $playerID) {
    $greetInfo = DB::connection($server)
    ->table('clients')
    ->where('id', $playerID)
    ->first();

    return view('servers.greeting')
    ->with('greetInfo', $greetInfo);
  }

  // Update greeting message
  public function updateGreeting(Request $request, $server, $playerID) {
      $gUpdate = DB::connection($server)
        ->table('clients')
        ->where('id', $playerID)
        ->update(['greeting' => $request->input('greet')]);

      return back()->with('success', 'Greeting updated successfully!');
  }

最后是表格

{{ Form::open(['action' => ['PlayerProfileController@updateGreeting', $greetInfo->greeting],  'method' => 'POST']) }}
  {{ Form::bsText('greet', '', ['placeholder' => 'Update greeting or leave blank to remove current message']) }}
  {{ Form::hidden('_method', 'PUT') }}
  <br>
  {{ Form::bsSubmit('Update',['class' => 'btn btn-outline-secondary']) }}
{!! Form::close() !!}

感谢任何和所有帮助。谢谢你的推荐。

1 个答案:

答案 0 :(得分:0)

您看到的ErrorException是由您尝试呼叫的路线缺少参数引起的。您的路线期待serverplayerId,您只需发送$greetInfo->greeting

当您使用命名路由时,我的建议是使用route()帮助器,如下所示:

Route::get('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController@greeting');
Route::post('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController@updateGreeting')
    ->name('greeting.update');


class PlayerProfileController extends Controller {

    public function greeting($server, $playerID) {
        $greetInfo = DB::connection($server)
            ->table('clients')
            ->where('id', $playerID)
            ->first();

        return view('servers.greeting')
            ->with('greetInfo', $greetInfo);
    }

    // Update greeting message
    public function updateGreeting(Request $request, $server, $playerID) {
        $gUpdate = DB::connection($server)
            ->table('clients')
            ->where('id', $playerID)
            ->update(['greeting' => $request->input('greet')]);

        return back()->with('success', 'Greeting updated successfully!');
    }

}

{{ Form::open(['action' => route('greeting.update', [$server, $playerId]),  'method' => 'post']) }}

    {{ Form::bsText('greet', '', ['placeholder' => 'Update greeting or leave blank to remove current message']) }}

    {{ Form::bsSubmit('Update',['class' => 'btn btn-outline-secondary']) }}

{!! Form::close() !!}

请将$server$playerId变量替换为代表您要随请求发送的参数的变量。

您可以看到我也删除了{{ Form::hidden('_method', 'PUT') }},因为它试图覆盖post方法上的Form::open方法。