laravelÔõÑùŲÓÃÄ£×Ó±äÁ¿
laravelÊÇÒ»¸öÊ¢ÐеÄphp¿ò¼Ü£¬ÌṩÁËÒ»Ì×Ç¿Ê¢µÄmodel-view-controller£¨mvc£©¼Ü¹¹£¬ÓÃÓÚ¿ìËÙ¹¹½¨webÓ¦ÓóÌÐò¡£ÔÚlaravelÖУ¬Ä£×ÓÊÇÖÎÀíÓ¦ÓóÌÐòÊý¾ÝµÄ½¹µã²¿·Ö¡£±¾ÎĽ«Ì½ÌÖÔõÑùÔÚlaravelÖÐŲÓÃÄ£×Ó±äÁ¿¡£
ÔÚLaravelÖУ¬ÓÐÁ½ÖÖÖ÷ÒªµÄÄ£×Ó±äÁ¿£ºÊµÀý±äÁ¿ºÍ¾²Ì¬±äÁ¿¡£ÊµÀý±äÁ¿ÊôÓÚÌض¨µÄÄ£×ÓʵÀý£¬²¢ÇÒÔÚÄ£×ÓÖнç˵¡£¾²Ì¬±äÁ¿ÊÇÔÚÄ£×ÓÀàÖнç˵£¬ÓëÈκÎÌض¨µÄÄ£×ÓʵÀýÎ޹ء£ÈÃÎÒÃÇ»®·ÖÌÖÂÛÕâÁ½ÖÖ±äÁ¿µÄʵÏÖ¡£
ʵÀý±äÁ¿
ʵÀý±äÁ¿Í¨³£ÓÃÓڴ洢ģ×ÓµÄÊôÐÔ£¬ÀýÈçÊý¾Ý¿â±íÖеÄ×ֶΡ£ÔÚLaravelÖУ¬ÔõÑùÔÚÄ£×ÓÖнç˵²¢»á¼ûʵÀý±äÁ¿£¿¾Ù¸öÀý×Ó£¬¼ÙÉèÎÒÃÇÓÐÒ»¸öUserÄ£×Ó£¬½ç˵ÁËÒ»¸öÃûΪnameµÄÊôÐÔ¡£ÎÒÃÇ¿ÉÒÔÔÚÄ£×ÓÀàÖн¨ÉèÒ»¸ö»ù±¾µÄ½á¹¹º¯Êý£¬ÎªnameÊôÐÔÖ¸¶¨Ä¬ÈÏÖµ¡£
<?php namespace AppModels; use IlluminateDatabaseEloquentModel; class User extends Model { public $name; public function __construct() { $this->name = 'John Doe'; } }
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÔÚ¿ØÖÆÆ÷»òÈκÎÆäËûµØ·½£¬ÎÒÃÇ¿ÉÒÔʵÀý»¯Ò»¸öUser¹¤¾ß£¬²¢»á¼û¸Ã¹¤¾ßµÄnameÊôÐÔ£º
$user = new User; $name = $user->name; // 'John Doe'
µÇ¼ºó¸´ÖÆ
ºÜ¼òÆÓ°É£¿ÎÒÃÇ»¹¿ÉÒÔͨ¹ý»á¼ûÒªÁìÀ´»ñÈ¡»òÉèÖÃʵÀý±äÁ¿¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÔÚÄ£×ÓÖÐÌí¼ÓÒ»¸ögetNameÒªÁìÀ´»ñÈ¡nameÊôÐÔµÄÖµ£º
public function getName() { return $this->name; }
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇ¿ÉÒÔŲÓøÃÒªÁìÀ´»ñȡʵÀý±äÁ¿µÄÖµ£º
$user = new User; $name = $user->getName(); // 'John Doe'
µÇ¼ºó¸´ÖÆ
ÎÒÃÇÒ²¿ÉÒÔͨ¹ýsetÒªÁìÀ´ÉèÖÃʵÀý±äÁ¿µÄÖµ£º
public function setName($name) { $this->name = $name; }
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇ¿ÉÒÔŲÓøÃÒªÁìÀ´ÉèÖÃʵÀý±äÁ¿µÄÖµ£º
$user = new User; $user->setName('Jane Doe'); $name = $user->getName(); // 'Jane Doe'
µÇ¼ºó¸´ÖÆ
¾²Ì¬±äÁ¿
¾²Ì¬±äÁ¿ÊÇÔÚÄ£×ÓÀàÖнç˵µÄ£¬ÓëÈκÎÌض¨µÄÄ£×ÓʵÀýÎ޹ء£¾²Ì¬±äÁ¿Í¨³£ÓÃÓÚ´æ´¢ÓëÄ£×ÓÀà×Ô¼ºÏà¹ØµÄÐÅÏ¢£¬ÀýÈç¸ÃÄ£×ÓÀàÊÇ·ñÆôÓÃÈíɾ³ý¡£ÔÚLaravelÖУ¬ÔõÑùÔÚÄ£×ÓÖнç˵²¢»á¼û¾²Ì¬±äÁ¿£¿
ÎÒÃÇ¿ÉÒÔÔÚÄ£×ÓÀàÖнç˵һ¸ö¾²Ì¬±äÁ¿£¬ÈçÏÂËùʾ£º
<?php namespace AppModels; use IlluminateDatabaseEloquentModel; class User extends Model { public static $softDeleteEnabled = true; // ... }
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷»òÈκÎÆäËûµØ·½»á¼û¸Ã¾²Ì¬±äÁ¿£º
$softDeleteEnabled = User::$softDeleteEnabled; // true
µÇ¼ºó¸´ÖÆ
ÔÚÀàÖпÉÒÔͨ¹ýself»òstaticÒªº¦×Ö»á¼û¾²Ì¬±äÁ¿¡£ÈôÊÇÎÒÃÇÏëÔÚÄ£×ÓÀàÖлá¼û¾²Ì¬±äÁ¿£¬¿ÉÒÔʹÓÃselfÒªº¦×Ö£º
class User extends Model { public static $softDeleteEnabled = true; public function isSoftDeleteEnabled() { return self::$softDeleteEnabled; } }
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇ¿ÉÒÔŲÓøÃÒªÁìÀ´¼ì²é¾²Ì¬±äÁ¿µÄÖµ£º
$user = new User; $softDeleteEnabled = $user->isSoftDeleteEnabled(); // true
µÇ¼ºó¸´ÖÆ
ÈôÊÇÎÒÃÇÏëÔÚ×ÓÀàÖлá¼û¾²Ì¬±äÁ¿£¬¿ÉÒÔʹÓÃstaticÒªº¦×Ö£º
class Admin extends User { public function isSoftDeleteEnabled() { return static::$softDeleteEnabled; } }
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔʵÀý»¯Ò»¸öAdmin¹¤¾ß£¬²¢Å²ÓÃisSoftDeleteEnabledÒªÁìÀ´¼ì²é¾²Ì¬±äÁ¿µÄÖµ£º
$admin = new Admin; $softDeleteEnabled = $admin->isSoftDeleteEnabled(); // true
µÇ¼ºó¸´ÖÆ
½áÂÛ
ÔÚLaravelÖУ¬Ä£×ÓÊÇÖÎÀíÓ¦ÓóÌÐòÊý¾ÝµÄ½¹µã²¿·Ö¡£ÊµÀý±äÁ¿ºÍ¾²Ì¬±äÁ¿ÊÇÁ½ÖÖ³£¼ûµÄÄ£×Ó±äÁ¿¡£ÊµÀý±äÁ¿Í¨³£ÓÃÓڴ洢ģ×ÓµÄÊôÐÔ£¬ÀýÈçÊý¾Ý¿â±íÖеÄ×ֶΡ£¾²Ì¬±äÁ¿ÔòÓÃÓÚ´æ´¢ÓëÄ£×ÓÀà×Ô¼ºÏà¹ØµÄÐÅÏ¢¡£ÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃÇ̽ÌÖÁËÔõÑùÔÚLaravelÖнç˵ºÍ»á¼ûÕâÁ½ÖÖ±äÁ¿¡£ÕÆÎÕÕâЩ֪ʶ¿ÉÒÔʹÎÒÃǸüºÃµØÃ÷È·ºÍ¿ª·¢LaravelÓ¦ÓóÌÐò¡£
ÒÔÉϾÍÊÇlaravelÔõÑùŲÓÃÄ£×Ó±äÁ¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡