app > Http > Controllers > API > V1 > BoardController.php
public function destroy($id)
{
//$this->authorize('isAdmin');
$board = $this->board->findOrFail($id);
$board->delete();
return $this->sendResponse($board, 'Post Delete');
}
routes > api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Route;
...
Route::namespace('App\\Http\\Controllers\\API\V1')->group(function () {
Route::get('profile', 'ProfileController@profile');
Route::put('profile', 'ProfileController@updateProfile');
Route::post('change-password', 'ProfileController@changePassword');
Route::get('tag/list', 'TagController@list');
Route::get('category/list', 'CategoryController@list');
Route::post('product/upload', 'ProductController@upload');
Route::post('board', 'BoardController@store'); // create
//Route::get('board/{id}', 'BoardController@show'); //read(상세페이지)
//Route::put('board/{id}', 'BoardController@update'); // update
// Route::delete('board/{id}', 'BoardController@destroy'); // delete -> 2022.01.20 수정 ~ 추가 안 해도 apiResources에 의해 자동으로 매핑됨
Route::apiResources([
'user' => 'UserController',
'product' => 'ProductController',
'category' => 'CategoryController',
'tag' => 'TagController',
'board' => 'BoardController', // 추가
]);
});
resources > js > components > board > Board.vue
<script>
export default{
...
methods: {
...
deletePost(id){
Swal.fire({
title: '글을 삭제하시겠습니까?',
//text: "You won't be able to revert this!",
showCancelButton: true,
confirmButtonColor: '#d33',
cancelButtonColor: '#3085d6',
confirmButtonText: '삭제'
}).then((result) => {
// Send request to the server
if (result.value) {
this.form.delete('api/board/'+id).then(()=>{
Swal.fire(
'삭제되었습니다.',
'Your file has been deleted.',
'success'
);
// Fire.$emit('AfterCreate');
this.loadPosts();
}).catch((data)=> {
Swal.fire("Failed!", data.message, "warning");
});
}
})
}
}, ...
}
</script>
글 목록에서 해당 글이 사라진 것을 확인할 수 있다.
DB에서 역시 해당 글이 삭제되었다.