【发布时间】:2018-12-24 20:19:43
【问题描述】:
在我基于 Laravel 5.6 和 Vue.js 2 的 CRUD webapp 中,添加、编辑和显示工作正常。但是删除不起作用,因为 axios.delete 没有将 id 发送到控制器。 Home.vue 中的 Console.log 显示正确的 id 和键值。以下是我在控制器中得到的代码和结果。请告诉我的错误。另外我真的需要在 web.php 中删除路由吗?
控制器
public function destroy(Sms $sms)
{
$myfile = fopen("newfile.txt", "w", true) or die("Unable to open file!");
$txt = "Print_r: ". print_r($sms) ."\r\nID: ". $sms->id;
//fwrite($myfile, $txt);
fwrite($myfile, print_r($sms, TRUE));
//die();
Sms::where('id',$sms->id)->delete();
}
首页.vue
del(key,id){
if(confirm("Are you sure?")){
this.loading = !this.loading
/*axios.delete(`/sms/${id}`)*/
axios.delete(`sms/${id}`, {params: {id: `${id}`}})
.then((response)=> {this.lists.splice(key,1);this.loading = !this.loading})
.catch((error)=> this.errors = error.response.data.errors)
console.log(`KEY:${key} ID:${id}`);
}
}
web.php
Route::delete('sms/{id}', 'smsController@destroy');
新文件.txt
应用\短信对象 ( [隐藏:受保护] => 数组 ( [0] => created_at [1] => updated_at )
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] =>
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
)
[original:protected] => Array
(
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
【问题讨论】:
-
而且类名不区分大小写,对吧?
-
不,这不是区分大小写的情况。
标签: laravel laravel-5 vue.js vuejs2 axios