iT邦幫忙

2025 iThome 鐵人賽

DAY 16
0
Modern Web

Laravel 是甚麼系列 第 16

關於ORM

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20250818/20119035ppTZmUxOsI.png

設定那些欄位可以被更新
在Models裡面增加檔案
在terminal下指令php artisan make:model CartItem
自動新增顯示
https://ithelp.ithome.com.tw/upload/images/20250818/20119035vrEANCpUgg.png
裡面的程式碼:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class CartItem extends Model
{
    use HasFactory;
}

下指令讓新增檔案可以快速被讀取composer dump-autoload
下指令php artisan tinker

https://ithelp.ithome.com.tw/upload/images/20250818/20119035snAIRhUERP.png
撈出全部資料 下指令CartItem::all()顯示

https://ithelp.ithome.com.tw/upload/images/20250818/20119035wbEsK8zrcj.png

https://ithelp.ithome.com.tw/upload/images/20250818/20119035864G4ro32Y.png

設定那些欄位可以更新’’表示都可以更新protected $guarded = [''];
https://ithelp.ithome.com.tw/upload/images/20250818/201190354KNwqfGN5b.png
設定那些欄位不顯示 protected $hidden =['updated_at'];

數量*10
https://ithelp.ithome.com.tw/upload/images/20250818/201190352W6HFehpuQ.png
設定其他關聯php artisan make:model Product
https://ithelp.ithome.com.tw/upload/images/20250818/20119035RtpyNlj86Z.png
再設定 php artisan make:model Cart
https://ithelp.ithome.com.tw/upload/images/20250818/20119035jPJixY32bN.png
設定CartItem 一對多的關係
https://ithelp.ithome.com.tw/upload/images/20250818/20119035osZLJ7XdSW.png
修改後的程式碼:
https://ithelp.ithome.com.tw/upload/images/20250818/20119035IpXVg6suBH.png

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class CartItem extends Model
{
   use HasFactory;

   protected $guarded = [''];   
   protected $appends = ['current_price'];

   public function getCurrentPriceAttribute()
   {
       return $this->quantity * 10;
   }
   public function product()
   {
       return $this->belongsTo(Product::class);
   }
   public function cart()
   {
       return $this->belongsTo(Cart::class);
   }

}
```

--------------

Cart裡面去設定雙向功能
原來:
![https://ithelp.ithome.com.tw/upload/images/20250818/20119035kkomoIpuUu.png](https://ithelp.ithome.com.tw/upload/images/20250818/20119035kkomoIpuUu.png)
修改後:
![https://ithelp.ithome.com.tw/upload/images/20250818/20119035nuV7IwharX.png](https://ithelp.ithome.com.tw/upload/images/20250818/20119035nuV7IwharX.png)



<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Cart extends Model
{
   use HasFactory;

   public function cartItems()
   {
       return $this->hasMany(CartItem::class);
   }

}


------------
Product裡面去設定雙向功能

原來:
![https://ithelp.ithome.com.tw/upload/images/20250818/20119035E3vqP4hdTm.png](https://ithelp.ithome.com.tw/upload/images/20250818/20119035E3vqP4hdTm.png)
修改後:
![https://ithelp.ithome.com.tw/upload/images/20250818/20119035euV9nuvs0v.png](https://ithelp.ithome.com.tw/upload/images/20250818/20119035euV9nuvs0v.png)

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
   use HasFactory;
   public function cartItems()
   {
       return $this->hasMany(CartItem::class);
   }
}



----------------


我們明天見~

上一篇
加入條件判斷
下一篇
假刪除
系列文
Laravel 是甚麼30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言