简体   繁体   English

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

[英]Passing a parameter from views to controller in Laravel

I am trying to create a delete page where I am supposed to pass the $id parameter to the controller in order to delete the user. 我试图创建一个删除页面,我应该在其中将$ id参数传递给控制器​​以便删除用户。 However, I get the following error: 但是,出现以下错误:

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

I have tried to check how to pass a parameter from the view to the controller but I have not been successful. 我试图检查如何将参数从视图传递到控制器,但没有成功。 Could you please help out 你能帮忙吗

My code is as follows: 我的代码如下:

user.blade.php 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 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 web.php

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

change your routes. 更改路线。

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

also pass the id to the url 还将ID传递给网址

<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>

Change the form action to something like: 将表单操作更改为:

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

And route to: 并路由至:

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

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

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