123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
-
- namespace App\Http\Controllers\Api\V1;
-
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\DB;
- use App\Model\Transportation;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Storage;
- use Validator;
- use Helper;
-
-
- class TransportationController extends Controller
- {
- public function __construct()
- {
- DB::enableQueryLog();
- }
-
- public function getAll()
- {
- $query = Transportation::get();
-
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
- public function getByUser($user_id)
- {
- $query = Transportation::where('user_id', Helper::hash($user_id, 'decode'))->findOrFail();
-
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
- public function postStore(Request $request)
- {
- $post = $request->all();
- // $file = $request->file('gambar');
- //dd($post);
- /**digunakan untuk set rule validator */
- $rules = [
- 'gambar' => 'required',
- // 'gambar' => 'required|file|max:1000',
- ];
- /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
- // $alert = [
- // 'file' => 'File maksimal 1MB'
- // ];
- // $validator = Validator::make($post, $rules, $alert);
- $validator = Validator::make($post, $rules);
-
- if ($validator->passes()) {
- /**menggunakan transaction */
- DB::beginTransaction();
-
- // $nama_file = time()."_".$file->getClientOriginalName();
-
- // $tujuan_upload = 'gambar';
-
- // //$file->move($tujuan_upload,$nama_file);
- // Storage::putFileAs('public/images/transportation/', $file, $nama_file );
-
- $query = new Transportation();
- $query->name = $request->name;
- $query->transportation_identity = $request->transportation_identity;
- $query->transportation_type = $request->transportation_type;
- $query->transportation_photo = Helper::base64_to_image($request->gambar, 'transportation');
- $query->user_id = 123456;
- $query->save();
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
- }
-
- public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
- {
- $response['response'] = array(
- 'status' => $status,
- 'message' => $msg,
- 'status_msg' => $status_msg,
- );
- $response['param'] = !empty($param) ? $param : '';
-
- $response['results'] = $result;
-
- return response()->json($response, 200);
- }
-
-
- }
|