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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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'))->findOrFail();
  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 postStore(Request $request)
  50. {
  51. $post = $request->all();
  52. // $file = $request->file('gambar');
  53. //dd($post);
  54. /**digunakan untuk set rule validator */
  55. $rules = [
  56. 'gambar' => 'required',
  57. // 'gambar' => 'required|file|max:1000',
  58. ];
  59. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  60. // $alert = [
  61. // 'file' => 'File maksimal 1MB'
  62. // ];
  63. // $validator = Validator::make($post, $rules, $alert);
  64. $validator = Validator::make($post, $rules);
  65. if ($validator->passes()) {
  66. /**menggunakan transaction */
  67. DB::beginTransaction();
  68. // $nama_file = time()."_".$file->getClientOriginalName();
  69. // $tujuan_upload = 'gambar';
  70. // //$file->move($tujuan_upload,$nama_file);
  71. // Storage::putFileAs('public/images/transportation/', $file, $nama_file );
  72. $query = new Transportation();
  73. $query->name = $request->name;
  74. $query->transportation_identity = $request->transportation_identity;
  75. $query->transportation_type = $request->transportation_type;
  76. $query->transportation_photo = Helper::base64_to_image($request->gambar, 'transportation');
  77. $query->user_id = 123456;
  78. $query->save();
  79. if($query){
  80. $result = $query;
  81. $res_status = true;
  82. $msg = 'Mendapatkan Data';
  83. $status_msg = $msg;
  84. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  85. }else {
  86. $res_status = false;
  87. $msg = 'Data tidak ditemukan';
  88. $status_msg = $msg;
  89. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  90. }
  91. }
  92. }
  93. public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
  94. {
  95. $response['response'] = array(
  96. 'status' => $status,
  97. 'message' => $msg,
  98. 'status_msg' => $status_msg,
  99. );
  100. $response['param'] = !empty($param) ? $param : '';
  101. $response['results'] = $result;
  102. return response()->json($response, 200);
  103. }
  104. }