No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TransportationController.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Model\Transportation;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Storage;
  9. use Validator;
  10. use Helper;
  11. class TransportationController extends Controller
  12. {
  13. public function __construct()
  14. {
  15. DB::enableQueryLog();
  16. }
  17. public function getAll()
  18. {
  19. $query = Transportation::get();
  20. if($query){
  21. $result = $query;
  22. $res_status = true;
  23. $msg = 'Mendapatkan Data';
  24. $status_msg = $msg;
  25. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  26. }else {
  27. $res_status = false;
  28. $msg = 'Data tidak ditemukan';
  29. $status_msg = $msg;
  30. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  31. }
  32. }
  33. public function getByUser($user_id)
  34. {
  35. $query = Transportation::where('user_id', Helper::hash($user_id, 'decode'))->get();
  36. if($query){
  37. $result = $query;
  38. $res_status = true;
  39. $msg = 'Mendapatkan Data';
  40. $status_msg = $msg;
  41. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  42. }else {
  43. $res_status = false;
  44. $msg = 'Data tidak ditemukan';
  45. $status_msg = $msg;
  46. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  47. }
  48. }
  49. public function getSingle($data_id)
  50. {
  51. $query = Transportation::where('id', Helper::hash($data_id, 'decode'))->get();
  52. if($query){
  53. $result = $query;
  54. $res_status = true;
  55. $msg = 'Mendapatkan Data';
  56. $status_msg = $msg;
  57. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  58. }else {
  59. $res_status = false;
  60. $msg = 'Data tidak ditemukan';
  61. $status_msg = $msg;
  62. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  63. }
  64. }
  65. public function postStore(Request $request)
  66. {
  67. $post = $request->all();
  68. // $file = $request->file('gambar');
  69. //dd($post);
  70. /**digunakan untuk set rule validator */
  71. $rules = [
  72. 'gambar' => 'required',
  73. // 'gambar' => 'required|file|max:1000',
  74. ];
  75. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  76. // $alert = [
  77. // 'file' => 'File maksimal 1MB'
  78. // ];
  79. // $validator = Validator::make($post, $rules, $alert);
  80. $validator = Validator::make($post, $rules);
  81. if ($validator->passes()) {
  82. /**menggunakan transaction */
  83. DB::beginTransaction();
  84. // $nama_file = time()."_".$file->getClientOriginalName();
  85. // $tujuan_upload = 'gambar';
  86. // //$file->move($tujuan_upload,$nama_file);
  87. // Storage::putFileAs('public/images/transportation/', $file, $nama_file );
  88. $query = new Transportation();
  89. $query->name = $request->name;
  90. $query->transportation_identity = $request->transportation_identity;
  91. $query->transportation_type = $request->transportation_type;
  92. $query->transportation_photo = Helper::base64_to_image($request->gambar, 'transportation');
  93. $query->user_id = 123456;
  94. $query->save();
  95. if($query){
  96. $result = $query;
  97. $res_status = true;
  98. $msg = 'Mendapatkan Data';
  99. $status_msg = $msg;
  100. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  101. }else {
  102. $res_status = false;
  103. $msg = 'Data tidak ditemukan';
  104. $status_msg = $msg;
  105. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  106. }
  107. }
  108. }
  109. public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
  110. {
  111. $response['response'] = array(
  112. 'status' => $status,
  113. 'message' => $msg,
  114. 'status_msg' => $status_msg,
  115. );
  116. $response['param'] = !empty($param) ? $param : '';
  117. $response['results'] = $result;
  118. return response()->json($response, 200);
  119. }
  120. }