sanca

laravel-join

Jun 21st, 2021
852
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $waktuMulai = '2021-06-01';
  2.         $waktuAkhir = '2021-06-05';
  3.  
  4. /** METODE 1 */
  5.  
  6. $getProducts = HealthProduct::select('id as productId', 'name as productName');
  7.         $getCatProduct = HealthCatProduct::select('id as catProductId', 'name as catProductName');
  8.         $getCreatedBy = User::select('id as createdBy', 'name as createdName');
  9.         $getUpdatedBy = User::select('id as updatedBy', 'name as updatedName');
  10.  
  11.         $getHealthPenggunaan = HealthPenggunaan::leftJoinSub($getProducts, 'products', function ($joinProducts) {
  12.             $joinProducts->on('health_penggunaan.health_product_id', '=', 'products.productId');
  13.         })
  14.             ->leftJoinSub($getCatProduct, 'catProducts', function ($joinCatProducts) {
  15.                 $joinCatProducts->on('products.productId', '=', 'catProducts.catProductId');
  16.             })
  17.             ->leftJoinSub($getCreatedBy, 'createdAccount', function ($joinCreated) {
  18.                 $joinCreated->on('health_penggunaan.created_by', '=', 'createdAccount.createdBy');
  19.             })
  20.             ->leftJoinSub($getUpdatedBy, 'updatedAccount', function ($joinCreated) {
  21.                 $joinCreated->on('health_penggunaan.updated_by', '=', 'updatedAccount.updatedBy');
  22.             });
  23. if (!empty($dateRange)) {
  24.             $getHealthPenggunaan->whereDate('health_penggunaan.waktu', '>=', $waktuMulai);
  25.             $getHealthPenggunaan->whereDate('health_penggunaan.waktu', '<=', $waktuAkhir);
  26.         }
  27.  
  28.         $healthPenggunaan = $getHealthPenggunaan->get();
  29.  
  30.  
  31. /** METODE 2 */
  32. $getCreatedBy = User::select('id as createdBy', 'name as createdName');
  33.             $getUpdatedBy = User::select('id as updatedBy', 'name as updatedName');
  34.  
  35.             $getHealthPenggunaan = HealthPenggunaan::leftJoinSub($getCreatedBy, 'createdAccount', function ($joinCreated) {
  36.                 $joinCreated->on('health_penggunaan.created_by', '=', 'createdAccount.createdBy');
  37.             })
  38.                 ->leftJoinSub($getUpdatedBy, 'updatedAccount', function ($joinCreated) {
  39.                     $joinCreated->on('health_penggunaan.updated_by', '=', 'updatedAccount.updatedBy');
  40.                 })
  41.                 ->leftJoin('health_products', 'health_penggunaan.health_product_id', '=', 'health_products.id')
  42.                 ->leftJoin('health_cat_products', 'health_products.cat_product_id', '=', 'health_cat_products.id');
  43.  
  44. if (!empty($dateRange)) {
  45.                 $getHealthPenggunaan->whereDate('health_penggunaan.waktu', '>=', $waktuMulai);
  46.                 $getHealthPenggunaan->whereDate('health_penggunaan.waktu', '<=', $waktuAkhir);
  47.             }
  48.  
  49. $healthPenggunaan = $getHealthPenggunaan->get();
  50.  
RAW Paste Data