在Laravel中将参数从视图传递到控制器

时间:2018-02-19 12:29:42

标签: php laravel laravel-5

我正在尝试创建一个删除页面,我应该将$ id参数传递给控制器​​以删除用户。但是,我收到以下错误:

Type error: Too few arguments to function App\Http\Controllers\mainController::deleteuser(), 0 passed and exactly 1 expected

我试图检查如何将视图中的参数传递给控制器​​,但我没有成功。你能帮忙吗

我的代码如下:

user.blade.php

 <div class="box-body">
              <table id="example2" class="table table-bordered table-hover">
                 <thead>
                  <tr>
                    <!-- <th></th> -->
                    <th>Username</th>
                    <th>Contact</th>
                    <th>Email</th>
                    <th>Role Type</th>
                    <th>Actions</th>
                  </tr>
                  </thead>
                  <tbody>
                  @foreach ($data as $datas)
                  <tr>
                    <td>{{ $datas->username }}</td>
                    <td>{{ $datas->contact }}</td>
                    <td>{{ $datas->email }}</td>
                    <td>Role Type</td>
                    <td>
                      <div class="btn-group">
                        <a href="" data-toggle="modal" data-target="#edit-modal"><i class="fa fa-edit" title="Edit"></i></a>
                        <a href="" data-toggle="modal" data-target="#delete-modal"><i class="fa fa-trash" title="Delete"></i></a>
                      </div>
                    </td>
                  </tr>
                  @endforeach
                  </tbody>
              </table>


<form role="form" action="/edit_user">
            <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
            <div class="box-body">
                <div class="form-group">
                  <label for="exampleInputEmail1">User ID</label> 
                  <input type="text" name="user_id" class="form-control" id="user_id">
                </div>
                <div class="form-group">
                  <label for="exampleInputEmail1">Username</label> 
                  <input type="text" class="form-control" name="username" placeholder="Enter username">
                </div>
                <div class="form-group">
                  <label for="exampleInputEmail1">Email</label> 
                  <input type="text" class="form-control" name="email" placeholder="Enter email">
                </div>
                <div class="form-group">
                  <label for="exampleInputEmail1">Contact</label> 
                  <input type="text" class="form-control" name="contact" placeholder="Enter contact">
                </div>
                <div class="form-group">
                  <label for="exampleInputEmail1">Change Password</label> 
                  <input type="password" class="form-control" name="change_password" placeholder="Enter password">
                </div>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary">Save changes</button>
              </div>
            </form>

          </div>
        </div>
      </div>
    </div>





     <!-- Delete User Modal -->
    <div class="modal fade" id="delete-modal">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" align="center"><b>Delete User</b></h4>
          </div>
          <div class="modal-body">
            <h4 align="center">Are you sure you want to delete this user?</h4>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
            <a href="{{ url('/delete')}}"><button type="submit" class="btn btn-danger">Delete User</button></a>
          </div>
        </div>
      </div>
    </div>  

Controller.php这样

public function deleteuser($id){
        DB::table('users')->where('userID', '=', $id)->delete();
        return redirect()->route('user_maintenance')->with('delete_user', 'User deleted successfully');
    }

web.php

Route::get('/delete', [ 'as' => 'delete', 'uses' => 'mainController@deleteuser']);

2 个答案:

答案 0 :(得分:1)

改变路线。

 Route::get('/delete/{id}', [ 'as' => 'delete', 'uses' => 'mainController@deleteuser']);

也将id传递给url

<div class="modal-footer">
        <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
        <a href="{{ url('/delete/your-id-to-delete')}}"><button type="submit" class="btn btn-danger">Delete User</button></a>
      </div>

答案 1 :(得分:0)

将表单操作更改为:

action="/edit_user/{{ $user->id }}">

前往:

Route::post('delete/{id}', [ 'as' => 'delete', 'uses' => 'mainController@deleteuser']);