2016-12-17 1 views
0

私の見解は、このようなものです:解決方法laravel 5.3のApp Http Controllers UserController :: destroy()の引数2が欠落していますか?

@foreach($users as $user) 
    <tr> 
     <td>{!! $user->id !!}</td> 
     <td>{!! $user->username !!}</td> 
     <td>{!! $user->phone !!}</td> 
     <td>{!! $user->address !!}</td> 
     <td> 
      {!! Form::open(['route' => ['users.destroy', $user->id], 'method' => 'delete']) !!} 
      <div class='btn-group'> 
       <a href="{!! route('users.edit', [$user->id]) !!}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-edit"></i></a> 
       {!! Form::button('<i class="glyphicon glyphicon-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!} 
      </div> 
      {!! Form::close() !!} 
     </td> 
    </tr> 
@endforeach 

マイルート\ web.phpは、このようなものです:

public function destroy($id, $year) 
{ 
    $user = $this->userRepository->findWithoutFail($id); 

    if (empty($user)) { 
     Flash::error('User not found'); 

     return redirect(route('users.index')); 
    } 

    $this->userRepository->delete($id); 

    Flash::success('User deleted successfully.'); 

    // return redirect(route('users.index')); 
    return redirect(route('users.index.year', ['year' => $year])); 
} 

のようなエラーが存在するがあります:

Route::get('users/destroy/{year}', '[email protected]')->name('users.destroy.year'); 

Route::resource('users', 'UserController'); 

私のコントローラは、このようなものですこれは:

ErrorException in UserController.php line 205: Missing argument 2 for App\Http\Controllers\UserController::destroy() 

とURLは次のようになります。クリックボタンを削除するとhttp://localhost/mysystem/public/users/10

、私は、URLは次のようになりたい:http://localhost/mysystem/public/users/index/2020

は私を助けることができるすべての人はありますか?

+0

あなたは ' 'ルート' => [ 'users.destroy'、$ USER-> ID]で同様年を提供する必要があります'、あなたがしています現在1つの議論が欠けている。 –

+0

@Joel Hinz、私はこのように変わります: '{!! Form :: open(['route' => ['users.destroy'、$ user-> id、$ year]、 'method' => 'delete'])!} '。ボタンの削除をクリックすると、URLは 'http:// localhost/mysystem/public/users/10?2020'のようになります。だから、それは動作していません –

+0

あなたはまた、ルートの定義からユーザーIDが不足しているように見えます。 –

答えて

0

たぶん、あなたは試すことができます。この

$year = 2020; 
{!! Form::open(['route' => ['users.destroy', $user->id, $year], 'method' => 'delete']) !!} 
+0

私はこのように変わります: '{!! Form :: open(['route' => ['users.destroy'、$ user-> id、$ year]、 'method' => 'delete'])!} '。ボタンの削除をクリックすると、URLは 'http:// localhost/mysystem/public/users/10?2020'のようになります。それで、うまくいきません。 –

0

を試してみてください:ルートで を/ web.phpはちょうどあなたが年の値で隠された入力を追加する形でのルートリソース

Route::resource('users', 'UserController'); 

を宣言

{!! Form::open(['route' => ['users.destroy', $user->id], 'method' => 'delete']) !!} 


<input type="hidden" name="year" value="2020"> 

コントローラを更新してください。

あなたの open方法で間違ったルート名を使用している
public function destroy(Request $request, $id) 
{ 
$user = $this->userRepository->findWithoutFail($id); 
$year = $request->year; 
if (empty($user)) { 
    Flash::error('User not found'); 

    return redirect(route('users.index')); 
} 

$this->userRepository->delete($id); 

Flash::success('User deleted successfully.'); 

// return redirect(route('users.index')); 
return redirect('users/index/'.$year); 

}

0

、それはusers.destroy.yearする必要があります。

だからあなたのフォームは、のようになります

{!! Form::open(['route' => ['users.destroy.year', $user->id, $year], 'method' => 'delete']) !!} 
+0

私はそれを変更しました。しかし、次のようなエラーが存在します: 'RouteCollection.php 218行目のMethodNotAllowedHttpExceptionと' urlのように見えます: 'http:// localhost/mysystem/public/users/destroy/2?2016' –

+0

あなたのルートを'delete'動詞を' Route :: delete(...) 'として使います。それがあなたの問題を解決すれば、それを試してください。 –

+0

私はhttp://labs.infyom.com/laravelgenerator/またはhttps://github.com/InfyOmLabs/adminlte-generator/tree/5.3を使用しています。削除が自動的に生成されたようです。ルートを追加する必要はありません。 –

関連する問題