我想要删除记录:在按钮单击(hasMany)时删除属于用户的例程。我已经建立了视图,模型和关系,删除路线以及要删除的控制器方法。从关系中删除记录
当我尝试点击按钮从数据库中删除例程时,它什么都不做。为什么它不删除记录?
这里是我的代码:路线: Route::post('routine/delete', '[email protected]'); // Delete a routine for a user.
控制器:
public function delete(Request $request)
{
$id = $request->input("id"); // Getting the id via. the ajax request.
$routine = \App\Routine::find($id); //Fetching the routine object from the db ifentified by the id passed via ajax
if ($routine)
{
$routine->delete();
}
return ["status" => "success"];
}
查看:
<div class="col-lg-2">
<!-- When this button is clicked, we determine which routine to remove. -->
<button class="btn btn-danger remove_routine" data-id="{{$routine->id}}" data-token="{{csrf_token()}}" style="display:inline">Delete</button>
</div>
用户模型:
public function routine()
{
return $this->hasMany('App\Routine');
}
常规模式:
public function user()
{
return $this->belongsTo('App\User');
}
在此先感谢!
你确定你通过ajax调用你想要删除的正确的id吗? – tanvirjahan
这个问题的ajax部分在哪里?浏览器控制台中的请求是否有任何错误?如果出现错误,您将能够在开发工具的网络部分查看laravel堆栈跟踪。 –
我想我错过了这里的ajax部分,因为我没有它。 – osherdo