iT邦幫忙

2025 iThome 鐵人賽

DAY 11
0
Modern Web

Laravel 是甚麼系列 第 11

資料庫關聯

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20250813/20119035XeaRiqTKvi.png
到資料表 sbl_teams
https://ithelp.ithome.com.tw/upload/images/20250813/20119035FlklIVMRm1.png
加入欄名
https://ithelp.ithome.com.tw/upload/images/20250813/201190351A8VbEMMr9.png

看資料表內容 多了一欄
https://ithelp.ithome.com.tw/upload/images/20250813/20119035C7aUICmZe7.png
修改內容owner_id這欄內容
https://ithelp.ithome.com.tw/upload/images/20250813/20119035G4mXnqcthY.png
以上是一對一跟一對多
https://ithelp.ithome.com.tw/upload/images/20250813/20119035jFm0VFqAZg.png
再來是 多對多
再新增一個資料表
https://ithelp.ithome.com.tw/upload/images/20250813/20119035Qb0Eb3p7ae.png
在資料表內加入內容:
https://ithelp.ithome.com.tw/upload/images/20250813/20119035bBTQ772CuS.png
一個球隊會有很多球員
一個球員屬於很多球隊
新增中間表:資料表
https://ithelp.ithome.com.tw/upload/images/20250813/20119035sQdEcz9VTD.png
加入內容
https://ithelp.ithome.com.tw/upload/images/20250813/20119035tV9Hr54EQI.png
加入join
FROM sampledatabase.sbl_team_data join sampledatabase.sbl_teams on sbl_team_data.team_id = sbl_teams.id ;
https://ithelp.ithome.com.tw/upload/images/20250813/20119035F309kS91Zf.png
FROM sampledatabase.sbl_team_data inner join sampledatabase.sbl_teams on sbl_team_data.team_id = sbl_teams.id ;
目前是inner join 寫法
沒有看到team_id有43的
https://ithelp.ithome.com.tw/upload/images/20250813/20119035kcL38f4GyQ.png
用left join 寫法
看到team_id有43的
SELECT * FROM sampledatabase.sbl_team_data left join sampledatabase.sbl_teams on sbl_team_data.team_id = sbl_teams.id ;
https://ithelp.ithome.com.tw/upload/images/20250813/20119035WmQzrtNsbv.png

https://ithelp.ithome.com.tw/upload/images/20250813/201190353qboEEqIje.png
先增加一個球隊名稱:天天快樂
https://ithelp.ithome.com.tw/upload/images/20250813/20119035zcsFVR8QWv.png
再用 right join:
SELECT * FROM sampledatabase.sbl_team_data right join sampledatabase.sbl_teams on sbl_team_data.team_id = sbl_teams.id ;
https://ithelp.ithome.com.tw/upload/images/20250813/20119035T0MYIV3YYg.png

做FK的限制
先把原來多增加的刪掉
https://ithelp.ithome.com.tw/upload/images/20250813/20119035oxtj01ixOJ.png
顯示
https://ithelp.ithome.com.tw/upload/images/20250813/20119035t8a3E9vyXw.png
選到
https://ithelp.ithome.com.tw/upload/images/20250813/201190358CWiFAfIuC.png
對應到的table
https://ithelp.ithome.com.tw/upload/images/20250813/201190354vLKBM9cUj.png

https://ithelp.ithome.com.tw/upload/images/20250813/20119035VkYpNVtbR9.png
再去測試新增10
https://ithelp.ithome.com.tw/upload/images/20250813/201190353I8AcuuRZj.png
按存檔會報錯
改成存5
就可以存檔成功
https://ithelp.ithome.com.tw/upload/images/20250813/20119035p3hKFsKj2d.png
設定串接mysql
https://ithelp.ithome.com.tw/upload/images/20250813/201190350LcZzITGYT.png
修改成
https://ithelp.ithome.com.tw/upload/images/20250813/20119035TJsdSnZsnp.png
下面的TERMINAL填入php artisan serve 啟動
https://ithelp.ithome.com.tw/upload/images/20250813/20119035uSziXsF4uV.png
要記得存檔
https://ithelp.ithome.com.tw/upload/images/20250813/20119035sMU3WMFOaU.png

改程式碼
https://ithelp.ithome.com.tw/upload/images/20250813/20119035dPRY8Oh04T.png
加入套件模組: use Illuminate\Support\Facades\DB;
將index改成連資料表$data = DB::table('sbl_teams')->get();

 public function index(Request $request)
   {
       //$data = $this->getDate();
       $data = DB::table('sbl_teams')->get();
       //dump($data);
       return response($data);
   }
   

用POSTMAN測試顯示
localhost:8000/products
https://ithelp.ithome.com.tw/upload/images/20250813/201190354f1vJWCXbI.png

預設顯示mysql的地方
https://ithelp.ithome.com.tw/upload/images/20250813/20119035OV4OYM2RY9.png

大家明天見


上一篇
資料庫的datatype
下一篇
後端程式碼接sql
系列文
Laravel 是甚麼30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言