SHARE
TWEET

Untitled

a guest Jul 24th, 2019 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 這次要解決的是如何在更新資料時,當傳進的值為null時,不會把null也存進db,只挑出有值的存入db.
  2.  
  3. 省思:
  4. 對於array的概念和key、value的概念還不夠通透,所以無法將'oneMonthRent' => $data['oneMonthRent'] 直接想到設一個$value[],
  5. 用 $value[$rent] = $data[$rent]來存.
  6.  
  7. 解法:
  8.  
  9. // 將key名稱存入array中
  10. $rents = [
  11.   'oneMonthRent',
  12.   'twoMonthsRent',
  13.   'threeMonthsRent',
  14.   'fourMonthsRent',
  15.   'fiveMonthsRent',
  16.   'sixMonthsRent',
  17.   'yearRent'
  18. ];
  19.  
  20. // 建立一個array,待會兒將有值的資料存入該陣列
  21. $values = [];
  22.  
  23. // 檢查是否有值
  24. foreach($rents as $rent) {
  25.   if (isset($data[$rent])) {
  26.     $value[$rent] = $data[$rent];
  27.   }
  28. }        
  29.  
  30. // 進行更新
  31. $this->listing
  32.   ->where('listingId', $listingId)
  33.   ->update($value);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top