You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.7 KiB
65 lines
1.7 KiB
<?php
|
|
|
|
namespace App\Admin\Actions\Post;
|
|
|
|
use App\Models\Product;
|
|
use Encore\Admin\Actions\RowAction;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ProductStatusAction extends RowAction
|
|
{
|
|
// 动作名称,显示在界面上的按钮文本
|
|
public $name = '';
|
|
|
|
/**
|
|
* 设置动作名称
|
|
*
|
|
* @param string $name 动作名称
|
|
* @return $this 返回当前实例,以便链式调用
|
|
*/
|
|
public function setName($name)
|
|
{
|
|
$this->name = $name; // 设置动作名称
|
|
|
|
return $this; // 返回当前实例
|
|
}
|
|
|
|
/**
|
|
* 处理商品状态的逻辑
|
|
*
|
|
* @param Product $product 当前商品模型实例
|
|
* @return \Encore\Admin\Actions\Response 返回操作结果的响应
|
|
*/
|
|
public function handle(Product $product)
|
|
{
|
|
// 检查商品是否已下架
|
|
if ($product->trashed()) {
|
|
// 如果商品已下架,则重新上架
|
|
$product->restore();
|
|
} else {
|
|
// 如果商品未下架,则进行下架操作
|
|
$product->delete();
|
|
}
|
|
|
|
// 返回成功响应并刷新页面
|
|
return $this->response()->success('操作成功.')->refresh();
|
|
}
|
|
|
|
/**
|
|
* 从请求中检索商品模型
|
|
*
|
|
* @param Request $request 当前请求实例
|
|
* @return Product|false 返回找到的商品模型实例,或返回 false
|
|
*/
|
|
public function retrieveModel(Request $request)
|
|
{
|
|
// 从请求中获取商品的唯一键
|
|
if (!$key = $request->get('_key')) {
|
|
return false; // 如果没有提供键,则返回 false
|
|
}
|
|
|
|
// 查找并返回商品模型,包括已删除的商品
|
|
return Product::query()->withTrashed()->findOrFail($key);
|
|
}
|
|
}
|