safriansah

laravel-crud-show

Sep 27th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. public function show(Request $request)
  2. {
  3.     //
  4.     try {
  5.         //code...
  6.         $param = $request->header('param');
  7.         if ($param) {
  8.             # code...
  9.            $param = json_decode($param, true);
  10.             $validator = Validator::make($param, [
  11.                 'id' => 'numeric',
  12.                 'id_user' => 'numeric',
  13.                 'title' => 'max:255',
  14.                 'min_amount' => 'numeric|min:0',
  15.                 'max_amount' => 'numeric|min:0',
  16.                 'type' => 'in:cr,db',
  17.                 'status' => 'in:0,1',
  18.                 'start_date' => 'date_format:Y-m-d',
  19.                 'end_date' => 'date_format:Y-m-d'
  20.             ]);
  21.            
  22.             if ($validator->fails()) {
  23.                 return $this->getResponse(406, $validator->errors()->first());
  24.             }
  25.  
  26.             $query = Transactions::query();
  27.             if (isset($param['id'])) $query->where('id', $param['id']);
  28.             if (isset($param['id_user'])) $query->where('id_user', $param['id_user']);
  29.             if (isset($param['title'])) $query->where('title', 'like', '%'.$param['title'].'%');
  30.             if (isset($param['type'])) $query->where('type', $param['type']);
  31.             if (isset($param['status'])) $query->where('status', $param['status']);
  32.             if (isset($param['min_amount'])) $query->where('amount', '>=', $param['min_amount']);
  33.             if (isset($param['max_amount'])) $query->where('amount', '<=', $param['max_amount']);
  34.             if (isset($param['start_date'])) $query->where('date', '>=', $param['start_date']);
  35.             if (isset($param['end_date'])) $query->where('date', '<=', $param['end_date']);
  36.             $transactions = $query->get();
  37.  
  38.             return $this->getResponse(200, false, $transactions);
  39.         }
  40.         else {
  41.             $transactions = Transactions::all();
  42.             return $this->getResponse(200, false, $transactions);
  43.         }
  44.     } catch (\Throwable $th) {
  45.         //throw $th;
  46.         // dd($th);
  47.         return $this->getResponse(500, $th);
  48.     }
  49. }
Add Comment
Please, Sign In to add comment