Laravel 删除不使用 API 路线

Laravel Delete not Working with API Route

我有这个控制器方法:

<?php

public function ignore($id)
{
    Log::info($id);
    $st = Servicetype::destroy($id);
    Log::info($st);
    // $st->delete();

    return response(null, Response::HTTP_OK);
}

此外,Api.php中的这条路线:

Route::delete('/servicetypelinking/{id}/ignore', 'PlanningCenter\Controllers\ServiceTypeLinkingController@ignore');

我正在从如下所示的 Vue.js 方法调用路由:

ignore(id) {
  console.log('In the ignore method: ' + id);
  this.mute = true;
  window.axios.delete('/api/servicetypelinking/' + id + '/ignore').then(({ response }) => {
    console.log(response);
    let index = this.serviceTypes.findIndex(serviceType => serviceType.id === id);
    this.serviceTypes.splice(index, 1);

    this.mute = false;
  });
}

Vue端正在运行,服务类型在屏幕上显示为"deleted"。数据库中没有发生任何事情。

当我 运行 id=16 时,我在 laravel.log 中得到这个,这表明正确的方法和正确的 id 是正确的:

[2018-11-26 17:27:42] local.INFO: 16  <-- This is passed in
[2018-11-26 17:27:42] local.INFO: 0   <-- Result of the destroy operation

我还可以在此模型上执行 Servicetype::find(16)->toArray() 并检索数据并将其显示在日志中。

如果我创建到此控制器方法的 Web 路由并使用 ID 进行获取,则删除发生在数据库中。出于某种原因,即使 id 正在获取方法,调用模型上的 destroy 方法也没有做任何事情。

这里有什么秘密 Laravel 吗?感谢任何帮助。

附加信息: Vue/javascript 方面似乎没有任何错误。控制台中没有错误,请求已正确发送(使用我的原始请求以及建议的 _delete 参数。)我已验证它正在访问该方法。我还将 id 硬编码到 destroy() 方法中,但它仍然没有删除记录。 laravel 错误日志中也没有错误。一切都应该有效,但事实并非如此。

解决方案:我没有真正找到问题的解决方案,所以我解决了它。使用

Servicetype::destroy($id);

什么也没做。但是,使用:

DB::table('servicetypes')->where('id', $id)->delete();

有效。我本来希望使用软删除,但发现了一些其他问题并使用了查询生成器,所以最后我做了一些重新编码来解决问题 "go away." 我可能会在有空的时候再深入研究一下。这是奇怪的行为。

试试这个:

axios.post('/api/servicetypelinking/' + id +  '/ignore', {_method: 'delete'})

在控制器中:

Servicetype::find($id)->delete();

如果不行,需要检查$id的记录。

Log:info( Servicetype::find($id) );

如果它是空的 – 那么问题是错误的记录 ID。

解决方案:我没有真正找到问题的解决方案,所以我解决了它。使用

Servicetype::destroy($id);

什么也没做。但是,使用:

DB::table('servicetypes')->where('id', $id)->delete();

有效。我本来希望使用软删除,但发现了一些其他问题并使用了查询生成器,所以最后我做了一些重新编码来解决问题 "go away." 我可能会在有空的时候再深入研究一下。这是一种奇怪的行为。我相应地更新了描述。