webman-admin/app/api/controller/DishesController.php
2025-07-23 14:46:58 +08:00

50 lines
1.4 KiB
PHP

<?php
namespace app\api\controller;
use app\common\controller\BaseLikeAdminController;
use app\common\model\Dishes;
use app\common\model\DishesCategory;
class DishesController extends BaseLikeAdminController
{
public $optional = ['category', 'dishes', 'detail'];
public function category()
{
$category = DishesCategory::field('id,name,pid')->select()->toArray();
$category = linear_to_tree($category, 'children');
return $this->data($category);
}
public function dishes()
{
$categoryId = $this->request->get('category_id');
$keyword = $this->request->get('keyword');
$query = Dishes::field('id,dishes_category_id,name,image,intro')->where('status', 1);
if (!empty($categoryId)) {
$query->where('dishes_category_id', $categoryId);
}
if (!empty($keyword)) {
$query->whereLike('name', "%$keyword%");
}
$data = $query->select()->toArray();
return $this->data($data);
}
public function detail()
{
$id = $this->request->get('id');
$data = Dishes::with(['dishesProduct' => function ($query) {
$query->with(['product', 'unit']);
}])->where('id', $id)->where('status', 1)->find();
if (empty($data)) {
return $this->fail('菜品不存在');
}
$data = $data->toArray();
return $this->data($data);
}
}