【问题标题】:Destroy() in Basic Controller is not working基本控制器中的 Destroy() 不起作用
【发布时间】:2020-03-16 00:03:22
【问题描述】:

所以我在表格中打印用户投诉,我还在每行打印一个删除按钮。当我单击该删除按钮时,我想从表中删除该特定投诉。我没有为此使用资源控制器,而是使用基本控制器。现在,这是我的代码:

ViewComplaint.blade.php(带删除按钮的投诉表):

<table id="cTable" class="table table-striped table-bordered">
   <thead>
      <tr>
         <th>Student Name</th>
         <th>Complaint Title</th>
         <th>Complaint Description</th>
         <th>Action</th>
      </tr>
   </thead>
   <tbody>
      @foreach($complaints as $complaint)
      <tr>
         <td>{{ $complaint->name }}</td>
         <td>{{ $complaint->cname }}</td>
         <td>{{ $complaint->cbody }}</td>
         <td class="btn-group">
            {!! Form::open(array('route'=>['complaint.destroy',$complaint->id],'method'=>'DELETE')) !!}
            {!! Form::submit('Delete',['type'=>'submit','style'=>'border-radius: 0px;','class'=>'btn btn-danger btn-sm',$complaint->id]) !!}
            {!! Form::close() !!}
         </td>
      </tr>
      @endforeach
   </tbody>
</table>

Web.php(路由):

Route::get('/complaint/create','ComplaintController@create')->name('complaint.create');
Route::post('/complaint','ComplaintController@store')->name('complaint.store');
Route::get('/complaint','ComplaintController@index')->name('complaint.index');
Route::delete('/complaint/{$complaint->id}','ComplaintController@destroy')->name('complaint.destroy');

ComplaintController.php(基本控制器):

class ComplaintController extends Controller
{
    public function index() {
        $complaints = Complaint::all();
        return view('viewcomplaint',compact('complaints'));
    }

    public function create(User $user) {
        $user = User::all();
        $user->name = Auth::user()->name;
        return view('createcomplaint',compact('user'));
    }

    public function store(Request $request, Complaint $complaint, User $user) {
        $user =  User::find($user);
        $complaint->name = Auth::user()->name;
        $complaint->cname = $request->input('cname');
        $complaint->cbody = $request->input('cbody');

        //update whichever fields you need to be updated
        $complaint->save();
        return redirect()->route('home.index');
    }

    public function destroy(Complaint $complaint,$id)
    {
        $complaint = Complaint::findOrFail($complaint->id);
        $complaint->delete();
        return redirect()->route('complaint.index');
    }
}

现在,当我单击表格上的删除按钮时,它只会给我“404 | Not Found”错误。我在这里做错了什么?非常感谢您的帮助。

【问题讨论】:

  • 您可以尝试从 destroy() 函数中删除 $id 参数并重试吗?
  • 试过了,同样的错误
  • 你使用的是什么版本的 Laravel?

标签: php laravel routes


【解决方案1】:

在我看来,您的路线定义错误。将您的路线更改为:

Route::delete('/complaint/{id}','ComplaintController@destroy')->name('complaint.destroy');

你不需要在你的表单打开中使用 array(),所以把你的表单打开挂到这个:

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

并从您的提交按钮中删除$complaint-&gt;id,您不需要它。

您现在在函数中要做的就是找到Complaint,其中包含您在表单中传递的id

public function destroy($id)
    {
        $complaint = Complaint::findOrFail($id);
        $complaint->delete();
        return redirect()->route('complaint.index');
    }

如果您发现任何错误,请告诉我。

【讨论】:

  • array(1, 2, 3)[1, 2, 3] 相同
  • 是的,只是更容易阅读,所以不需要指定array()方法
  • 在您的示例中更容易忘记将结束 ] 放在您的数组上;)
【解决方案2】:

路由参数只是一个名字;你是说这个特定的路线段是动态的,我想要名为complaint的参数:

Route::delete('complaint/{complaint}', 'ComplaintController@destroy')->name('complaint.destroy');

然后您可以调整您的destroy 方法以将参数complaint 类型提示为Complaint $complaint 以获得隐式绑定:

public function destroy(Complaint $complaint)
{
    $complaint->delete();

    return redirect()->route('complaint.index');
}

【讨论】:

  • 我在按钮中传递什么?因为如果我仍在传递投诉->id,则 URL 变为“/complaint/{complaint->id}”并显示“未找到”。
  • 你没有在按钮中传递任何东西,表单是定义动作 url 的东西
  • 单击删除时仍然出现 404 错误。这是我的表单:{!! Form::open(array('route'=&gt;['complaint.destroy',$complaint],'method'=&gt;'DELETE')) !!} {!! Form::submit('Delete',['type'=&gt;'submit','style'=&gt;'border-radius: 0px;','class'=&gt;'btn btn-danger btn-sm']) !!} {!! Form::close() !!},这是我的控制器:public function destroy(Complaint $complaint) { $complaint-&gt;delete(); return redirect()-&gt;route('complaint.index'); }
  • 当你到达这个 404 页面后会发生什么?您是否在浏览器中点击返回?
  • 我要回到 url /complaint,重新加载然后再次点击删除。
【解决方案3】:

从路由中移除 $id

Route::delete('/complain/{id}','ComplaintController@destroy')->name('complaint.destroy');

public function destroy($id) {

}

【讨论】:

  • 当我使用 $id 而不是 $complaint->id 时,它会显示“未定义的变量”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多