Forked from martindilling/2015_09_03_142321_create_coupons_table.php
Last active
September 3, 2015 20:33
-
-
Save parweb/9ec057686377ed755dc0 to your computer and use it in GitHub Desktop.
Polymorphic Coupons with Eloquent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateCouponsTable extends Migration | |
{ | |
public function up() | |
{ | |
Schema::create('coupons', function (Blueprint $table) { | |
$table->increments('id'); | |
$table->string('type'); | |
$table->string('code')->unique(); | |
$table->integer('value')->nullable(); | |
$table->integer('min_quantity')->nullable(); | |
$table->timestamps(); | |
}); | |
} | |
public function down() | |
{ | |
Schema::drop('coupons'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Coupon extends Model | |
{ | |
protected $table = 'coupons'; | |
public function __construct(array $attributes = []) | |
{ | |
parent::__construct($attributes); | |
$this->type = static::class; | |
} | |
public static function findByCode($code) | |
{ | |
$type = self::whereCode($code)->value('type'); | |
return (new $type)->whereCode($code)->first(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Seeder; | |
class CouponSeeder extends Seeder | |
{ | |
public function run() | |
{ | |
\App\Coupon::create([ | |
'code' => '001', | |
'value' => '0' | |
]); | |
\App\FixedValueCoupon::create([ | |
'code' => 'AA1', | |
'value' => '1000' | |
]); | |
\App\PercentOffCoupon::create([ | |
'code' => 'BB1', | |
'value' => '15' | |
]); | |
\App\MinimumQuantityCoupon::create([ | |
'code' => 'CC1', | |
'value' => '15', | |
'min_quantity' => 2 | |
]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class FixedValueCoupon extends Coupon | |
{ | |
public function discount() | |
{ | |
return 'Fixed value'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class MinimumQuantityCoupon extends Coupon | |
{ | |
public function discount() | |
{ | |
return 'Minimum quantity'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class PercentOffCoupon extends Coupon | |
{ | |
public function discount() | |
{ | |
return 'Percent off'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::get('/', function () { | |
$coupon = \App\Coupon::findByCode('001'); | |
var_dump($coupon->toArray()); | |
$fixedValue = \App\Coupon::findByCode('AA1'); | |
var_dump($fixedValue->toArray()); | |
$percentOff = \App\Coupon::findByCode('BB1'); | |
var_dump($percentOff->toArray()); | |
$minimumQuantity = \App\Coupon::findByCode('CC1'); | |
var_dump($minimumQuantity->toArray()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment