| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- /*
- * @Author: your name
- * @Date: 2021-04-22 17:20:17
- * @LastEditTime: 2021-07-21 14:35:56
- * @LastEditors: Please set LastEditors
- * @Description: In User Settings Edit
- * @FilePath: \10dsm\app\Http\Controllers\Admins\IndustryController.php
- */
- namespace App\Http\Controllers\Admins;
- use App\Modes\Industry;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- class IndustryController extends Controller
- {
- public function index(Request $request)
- {
- $params=$request->all();
- if(!isset($params['page'])){
- $res = Industry::whereStatus(1)->select(['id', 'content', 'type'])->orderBy('id','asc')->get();;
- }else{
- $res = Industry::whereStatus(1)->select(['id', 'content', 'type'])->orderBy('id','asc')->paginate(perPage());;
- }
- return showJsonSucc('查询成功',$res);
- }
- //添加类型 2021/7/21
- public function add(Request $request)
- {
- $validator = \Validator::make($param = $request->all(), [
- 'content' => 'required',
- 'type' => 'required|numeric|between:1,2'
- ]);
- if ($validator->fails()) {
- return showJsonErr($validator->errors()->first());
- }
- $result = Industry::whereContent($param['content'])->first();
- if($result){
- return showJsonErr('分类已存在');
- }
- $item = new Industry();
- $item->content = $param['content'];
- $item->type = $param['type'];
-
- if($item->save()){
- return showJsonSucc('添加类型成功');
- }else{
- return showJsonError('添加类型失败');
- }
- }
- //删除/批量删除 2021/7/21
- public function delete(Request $request)
- {
- $validator = \Validator::make($param = $request->all(), [
- 'id' => 'required',
- ]);
- if ($validator->fails()) {
- return showJsonErr($validator->errors()->first());
- }
- $ids = explode(',',$param['id']);
-
- \DB::beginTransaction();
- try {
- foreach ($ids as $id) {
- //查询数据是否存在
- $Industry = Industry::whereId($id)->first();
- if (empty($Industry)) {
- return showJsonErr("抱歉,【id:{$id}】不存在");
- }
- //修改数据状态
- $Industry->status = 2;
-
- if(!$Industry->save()){
- return showJsonErr("抱歉,删除失败");
- }
- }
- \DB::commit();
- return showJsonSucc('删除成功');
- } catch (\Exception $e) {
- \DB::rollBack();
- return showJsonErr($e->getMessage());
- }
- }
-
- }
|