Step 1: thực hiện chức năng Xóa (destroy)
Mô hình hoạt động của destroy:
Viết code cho action destroy($id) :
- Action
destroy($id) thường dùng để thực thi câu lệnh DELETE.
- Lưu ý: cần xóa các file liên quan đến Sản phẩm cần xóa (để tránh file rác trên hệ thống)
Hiệu chỉnh file app\Http\Controllers\Backend\SanphamController.php
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
// Tìm object Sản phẩm theo khóa chính
$sp = SanPham::where("sp_ma", $id)->first();
// Nếu tìm thấy được sản phẩm thì tiến hành thao tác DELETE
if(empty($sp) == false)
{
// Xóa hình cũ để tránh rác
Storage::delete('public/photos/' . $sp->sp_hinh);
}
$sp->delete();
// Hiển thị câu thông báo 1 lần (Flash session)
Session::flash('alert-info', 'Xóa sản phẩm thành công ^^~!!!');
// Điều hướng về trang index
return redirect()->route('admin.sanpham.index');
}
|