Laradebut #5 - 關於 CRUD 外的一點小事

Cara Wang <caraw@cnyes.com>

Laradebut #5

關於 CRUD 外的一點小事

@author Cara Wang <caraw@cnyes.com>

@since 2016/11/29

PHP Developer

Why Do We Need Framework in PHP ?

CodeIgniter

Phalcon

Phalcon MVC Examples

Laravel

Why Do We Need Framework in PHP ?

  1. Framework 幫忙做了哪些事 ?
    1. 目錄結構

CodeIgniter

Phalcon

Laravel

Why Do We Need Framework in PHP ?

  1. Framework 幫忙做了哪些事 ?
    1. 目錄結構
      1. 方便協同合作: 在同樣的規範與流程下寫程式

CodeIgniter

Laravel

Why Do We Need Framework in PHP ?

  1. Framework 幫忙做了哪些事 ?
    1. 目錄結構
      1. 方便協同合作: 在同樣的規範下寫程式
    2. 不用重複造輪子

Why Do We Need Framework in PHP ?

Why Do We Use Laravel ?

Why Do We Use Laravel ?

  1. 跟你的 Framework 學習: 跟著 Laravel 5.1 一起成為更好的開發者
    1. 快速的跟上語言版本特性 -> PHP7
      1. OCTOBER 26, 2016 - V4.2.20 is now released with support for PHP 7.0

Why Do We Use Laravel ?

  1. 跟你的 Framework 學習: 跟著 Laravel 5.1 一起成為更好的開發者
    1. 快速的跟上語言版本特性 -> PHP7
      1. OCTOBER 26, 2016 - V4.2.20 is now released with support for PHP 7.0
    2. 遵循標準: PSR-2 & PSR-4

Why Do We Use Laravel ?

  1. 跟你的 Framework 學習: 跟著 Laravel 5.1 一起成為更好的開發者
    1. 快速的跟上語言版本特性 -> PHP7
      1. OCTOBER 26, 2016 - V4.2.20 is now released with support for PHP 7.0
    2. 遵循標準: PSR-2 & PSR-4
    3. 整合套件管理工具: Composer & npm

Why Do We Use Laravel ?

By Josh Lockhart @ 2016 PHPConf Taiwan

Introduction to Eloquent

Introduction to Eloquent

  1. Laravel 提供的 ORM
  2. Why Do We Need ORM ?

Introduction to Eloquent

			<?php
			namespace App;
			use Illuminate\Database\Eloquent\Model;
			class Product extends Model
			{
			}
		

Introduction to Eloquent

  1. 如何使用
			Product::select('name', 'price')
			    ->where('discount', '<', 1)
			    ->first();
		

Introduction to Eloquent

  1. 如何使用
			SELECT `name`, `price`
			FROM `product`
			WHERE `discount` < 1;
		

Introduction to Eloquent

			CREATE TABLE `products` (
			    `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '商品流水號',
			    `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '商品名稱',
			    `brand` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '品牌',
			    `type` varchar(255) COLLATE utf8_unicode_ci DEFAULT '' COMMENT '商品型號',
			    `price` double(8,2) NOT NULL,
			    `discount` double(8,2) NOT NULL DEFAULT '1.00',
			    `created_at` timestamp NULL DEFAULT NULL,
			    `updated_at` timestamp NULL DEFAULT NULL,
			    PRIMARY KEY (`id`),
			) ENGINE=InnoDB AUTO_INCREMENT=1058 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
		

Introduction to Eloquent

			public function getPriceAttribute()
 			{
			    $price = (float)$this->attributes['price'];
			    $discount = (float)$this->attributes['discount'];
			    return (int) ceil($price * $discount);
			}
		

Introduction to Eloquent

			public function setDiscountAttribute($value)
			{
			    if ($value > 1) {
			        $value = 1;
			    } elseif ($value < 0.5) {
			        $value = 0.5;
			    }
			 
			    $this->attributes['discount'] = $value;
			}
		

Introduction to Eloquent

			public function setTypeAttribute($value)
			{
			    if (((int)stripos($value, '色') - 1) !== mb_strlen($value)
			            && in_array($value, self::$colors)) {
			        $value .= '色';
			    }
			 
			    $this->attributes['type'] = $value;
			}
		

Introduction to Eloquent

Introduction to Eloquent

			public function getFullNameAttribute()
 			{
			    return sprintf(
			        '【%s】%s - %s',
			        $this->attributes['brand'],
			        $this->attributes['name'],
			        $this->attributes['type']
			    );
			}
		

Introduction to Eloquent

			protected $appends = ['fullName'];
		

Introduction to Eloquent

			protected $attributes = [
			    'brand' => 'Laradebut'
			    'type' => ''
			];
		

Introduction to Eloquent

  1. 其他關於 Eloquent 的小事
    1. 設定 attributes 預設值
			protected $casts = [
			    'price'    => 'int',
			    'discount' => 'float'
			];
		

Introduction to Eloquent

  1. 其他關於 Eloquent 的小事
    1. 設定 attributes 預設值
    2. 設定 casts 資料型別
      1. price 必須是 integer

Illuminate/Database/Eloquent/Model

Introduction to Eloquent

  1. 其他關於 Eloquent 的小事
    1. 設定 attributes 預設值
    2. 設定 casts 資料型別
      1. price 必須是 integer
			const CREATED_AT = 'createdAt';
			const UPDATED_AT = 'updatedAt';
		

Introduction to Eloquent

  1. 其他關於 Eloquent 的小事
    1. 設定 attributes 預設值
    2. 設定 casts 資料型別
      1. price 必須是 integer
    3. 設定 CREATED_ATUPDATED_AT

原本:

			$table->timestamps();
		

更動 CREATED_ATUPDATED_AT:

			$table->timestamp('createdAt')->nullable();
			$table->timestamp('updatedAt')->nullable();
		

Why Do We Use Laravel ?

Q & A

2015/09 ~ 2016/12

2016/03

2016/05

2016/08

2016/12

如果你對我們有興趣,歡迎來找彩蛋

Fork me on GitHub