CategoryService.php
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 逻辑层:Category 服务类,处理业务逻辑
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Services
* @package App\Services
* @author Richer <yangzi1028@163.com>
* @date 2023年4月20日16:28:41
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Services;
use App\Models\Category\Category;
/**
* Class CategoryService
* @package App\Services
*/
class CategoryService extends BaseService
{
/**
* CategoryService constructor.
*
* @param Category $model
*/
public function __construct(Category $model)
{
// 执行父类构造方法
parent::__construct($model);
}
/**
* Display a listing of the resource.
*
* @return array|\Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection
*/
public function list()
{
return $this->model->with(['children'])
->where(function ($query) {
$query->whereNull('pid')->orWhere('pid', 0)->orWhere('pid', '');
})
->when($q = request('name'), function ($query) use ($q) {
$query->where("name", 'like', "%$q%");
})
->where('status', 1)
->orderByDesc('sort')
->groupBy('id')
->latest()
->get();
}
/**
* @param $id
*/
public function examples($id)
{
$model = $this->model->findOrFail($id);
// $list = $model->examples()->with(['answer:id,pid,body,created_at'])
// ->where('pid', 0)
// ->latest()
// ->paginate(request('per_page', config('constants.PER_PAGE')), ['id','category_id','body','created_at']);
$model = $this->model->findOrFail($id);
$list = $model->examples()
->latest()
->paginate(request('per_page', config('constants.PER_PAGE')), ['id','category_id','question','answer','created_at']);
return $list;
}
/**
* @param $id
*/
public function labels($id)
{
$model = $this->model->findOrFail($id);
$list = $model->labels()->with(['items'])
->latest()
->get()->map(function ($label) {
return [
'id' => (int) $label->id,
'category_id' => (int) $label->category_id,
'name' => (string) $label->name,
'description' => (string) $label->description,
'can_multiple' => (int) $label->can_multiple,
'can_multiple_show' => (string) $label->can_multiple_show,
'created_at' => format_date($label->created_at),
'items' => $label->items->map(function ($item) {
return [
'id' => (int) $item->id,
'name' => (string) $item->name,
'description' => (string) $item->description,
'created_at' => format_date($item->created_at),
];
})->toArray()
];
});
return $list;
}
}