資料庫課程作業筆記 - 編寫模型檔案

MARTINPOTTER發表於2019-04-24

相關參考

Laravel 文件 - Eloquent:入門

建立模型檔案

將模型檔案放到Models檔案下,名稱空間使用 App\Models

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
php artisan make:model Models/Employee 
php artisan make:model Models/Customer
php artisan make:model Models/Product
php artisan make:model Models/Purchase
php artisan make:model Models/Supplier

模型檔案主要部分

員工

class Employee extends Model
{
    // 規定表名稱,繼承填寫時預設以小寫複數作為表名(Laravel 底層約定)
    protected $table = 'employees';

    // 欄位名稱,以常量寫出起到全域性更改的作用
    const TABLE = 'employees';
    const ID = 'id';
    const NAME = 'ename';
    const CITY = 'city';

    // fillable 欄位用於限制哪些可以被批量賦值,在建立和修改時起到保護作用
    protected $fillable = [
        self::NAME,
        self::CITY
    ];

    // 這裡是一對多關係 一個員工擁有多個購買記錄
    public function purchases()
    {
        return $this->hasMany(Purchase::class,'eid','id');
    }
}

顧客

class Customer extends Model
{
    protected $table = 'customers';
    const TABLE = 'customers';
    const ID = 'id';
    const NAME = 'cname';
    const CITY = 'city';
    const VISITS_MADE = 'visits_made';
    const LAST_VISIT_TIME = 'last_visit_time';

    protected $fillable = [
        self::NAME,
        self::CITY,
        self::VISITS_MADE,
        self::LAST_VISIT_TIME
    ];

    public function purchases()
    {
        return $this->hasMany(Purchase::class,'cid','id');
    }
}

供應商

class Supplier extends Model
{
    protected $table = 'suppliers';
    const TABLE = 'suppliers';
    const ID = 'id';
    const NAME = 'sname';
    const CITY = 'city';
    const TELEPHONE = 'telephone_no';

    protected $fillable = [
        self::NAME,
        self::CITY,
        self::TELEPHONE
    ];

    public function products()
    {
        return $this->hasMany(Product::class,'sid','id');
    }
}

產品

class Product extends Model
{
    protected $table = 'products';
    const TABLE = 'products';
    const ID = 'id';
    const NAME = 'pname';
    const QOH = 'qoh';
    const QOH_THRESHOLD = 'qoh_threshold';
    const ORIGINAL_PRICE = 'original_price';
    const DISCNT_RATE = 'discnt_rate';
    const SID = 'sid';

    protected $fillable = [
        self::NAME,
        self::QOH,
        self::QOH_THRESHOLD,
        self::ORIGINAL_PRICE,
        self::DISCNT_RATE,
        self::SID
    ];

    public function supplier()
    {
        return $this->belongsTo(Supplier::class,'sid','id');
    }

    public function purchases()
    {
        return $this->hasMany(Purchase::class,'pid','id');
    }
}

購買記錄

class Purchase extends Model
{
    protected $table = 'purchases';
    const TABLE = 'purchases';
    const ID = 'id';
    const CID = 'cid';
    const EID = 'eid';
    const PID = 'pid';
    const QTY = 'qty';
    const PTIME = 'ptime';
    const TOTAL_PRICE = 'total_price';

    protected $fillable = [
        self::CID,
        self::EID,
        self::PID,
        self::QTY,
        self::PTIME,
        self::TOTAL_PRICE
    ];

    public function customer()
    {
        return $this->belongsTo(Customer::class,'cid','id');
    }

    public function employee()
    {
        return $this->belongsTo(Employee::class,'eid','id');
    }

    public function product()
    {
        return $this->belongsTo(Product::class,'pid','id');
    }

}

這裡沒有完成Log表的操作,將在下一個實驗完成,在建立完模型後我們去填充資料庫

MARTINPOTTER

相關文章