×ðÁú¿­Ê±

laravelÔõô·µ»ØÖ¸¶¨ÁÐ

laravelÊÇÒ»¿îÊ¢ÐеÄphp¿ò¼Ü £¬ËüÄܹ»×ÊÖúweb¿ª·¢ÕßÇáËɹ¹½¨¸ßЧµÄwebÓ¦ÓóÌÐò¡£ÔÚlaravelÖÐ £¬·µ»ØÖ¸¶¨ÁÐÊǷǾ­³£¼ûµÄÐèÇó¡£±¾ÎĽ«ÏÈÈÝlaravelÔõÑù·µ»ØÖ¸¶¨ÁС£

ÔÚLaravelÖÐ £¬ÈôÊÇÏëÒª·µ»ØÖ¸¶¨ÁÐ £¬¿ÉÒÔʹÓÃEloquentÄ£×Ó¡£EloquentÊÇLaravelµÄORM£¨¹¤¾ß¹ØϵӳÉ䣩¿ò¼Ü £¬ËüΪÎÒÃÇÌṩÁËÒ»ÖÖ¼òÆÓ¡¢ÓÅÑŵķ½·¨À´ÖÎÀíÊý¾Ý¿â¡£ÒÔÏÂÊÇʹÓÃEloquentÄ£×ÓÔõÑù·µ»ØÖ¸¶¨Áеİ취£º

°ì·¨Ò»£º½ç˵EloquentÄ£×Ó

Ê×ÏÈ £¬ÎÒÃÇÐèÒª½ç˵һ¸öEloquentÄ£×Ó¡£ÔÚLaravelÓ¦ÓóÌÐòÖÐ £¬ËùÓеÄEloquentÄ£×Ó¶¼Î»ÓÚappĿ¼ÏµÄModelsÎļþ¼ÐÖС£ÎÒÃÇ¿ÉÒÔͨ¹ýʹÓÃartisanÏÂÁîÐй¤¾ßÀ´½¨ÉèÒ»¸öеÄÄ£×Ó £¬ÈçÏÂËùʾ£º

php artisan make:model User

µÇ¼ºó¸´ÖÆ

Õ⽫ÔÚModelsÎļþ¼ÐÖн¨ÉèÒ»¸öÃûΪUserµÄEloquentÄ£×Ó¡£ÎÒÃÇ¿ÉÒÔÔÚ´ËÄ£×ÓÖнç˵×ðÁú¿­Ê±Êý¾Ý¿â±íºÍ×ֶΡ£

°ì·¨¶þ£º½ç˵Êý¾Ý¿â×Ö¶Î

½ÓÏÂÀ´ £¬ÎÒÃÇÐèÒªÔÚEloquentÄ£×ÓÖнç˵×ðÁú¿­Ê±Êý¾Ý¿â±íºÍ×ֶΡ£ÎÒÃÇ¿ÉÒÔÔÚUserÄ£×ÓÖнç˵×ðÁú¿­Ê±Êý¾Ý¿â±íºÍ×ֶΠ£¬ÈçÏÂËùʾ£º

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class User extends Model
{
    use HasFactory;

    protected $table = 'users';

    protected $fillable = [
        'name',
        'email',
        'password',
    ];
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖÐ £¬ÎÒÃÇͨ¹ýʹÓÃ$tableÊôÐÔÀ´½ç˵Êý¾Ý¿â±íµÄÃû³Æ £¬$fillableÊôÐÔÀ´½ç˵¿ÉÒÔÌî³äµ½Êý¾Ý¿âµÄ×ֶΡ£

°ì·¨Èý£º·µ»ØÖ¸¶¨ÁÐ

ÏÖÔÚ £¬ÎÒÃÇÒѾ­½ç˵ÁËEloquentÄ£×ÓºÍÊý¾Ý¿â×ֶΠ£¬ÎÒÃÇÖ»ÐèÒªÔÚ×ðÁú¿­Ê±¿ØÖÆÆ÷ÖÐŲÓÃÊý¾Ý¿âÅÌÎÊ £¬²¢·µ»ØÖ¸¶¨Áм´¿É¡£ÀýÈç £¬ÈôÊÇÎÒÃÇÖ»Ïë»ñÈ¡Óû§µÄEmailµØµã £¬ÎÒÃÇ¿ÉÒÔͨ¹ýÔÚ×ðÁú¿­Ê±¿ØÖÆÆ÷ÖÐŲÓÃÈçÏ´úÂëÀ´ÊµÏÖ£º

namespace AppHttpControllers;

use AppModelsUser;

class UserController extends Controller
{
    public function index()
    {
        $users = User::select('email')->get();

        return $users;
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖÐ £¬ÎÒÃÇʹÓÃÁËselectÒªÁìÀ´Ñ¡ÔñÎÒÃÇÒª·µ»ØµÄ×ֶΡ£ÔÚ×ðÁú¿­Ê±Àý×ÓÖÐ £¬ÎÒÃÇֻѡÔñÁËemail×ֶΡ£ÎÒÃÇ»¹Ê¹ÓÃÁËgetÒªÁìÀ´»ñÈ¡ËùÓÐÓû§µÄEmailµØµã¡£×îºó £¬ÎÒÃǽ«Ð§¹û·µ»Ø¸ø×ðÁú¿­Ê±ÊÓͼ»òÕßÒÔJSONÃûÌ÷µ»Ø¸øAPI¡£

³ýÁËʹÓÃselectÒªÁì £¬ÎÒÃÇ»¹¿ÉÒÔʹÓÃpluckÒªÁìÀ´Ö»·µ»ØÒ»¸öÌض¨µÄ×ֶΠ£¬ÈçÏÂËùʾ£º

$email = User::pluck('email');

µÇ¼ºó¸´ÖÆ

×ܽá

ÔÚLaravelÖÐ £¬Ê¹ÓÃEloquentÄ£×Ó¿ÉÒÔÀû±ãµØ·µ»ØÖ¸¶¨µÄÁС£ÎÒÃÇ¿ÉÒÔʹÓÃselectÒªÁìÀ´Ñ¡ÔñÎÒÃÇÒª·µ»ØµÄ×ֶΠ£¬»òÕßʹÓÃpluckÒªÁìÀ´·µ»ØÒ»¸öÌض¨µÄ×ֶΡ£ÕâʹµÃLaravelÔÚ´¦Öóͷ£´óÐÍÊý¾Ý±íʱԽ·¢¸ßЧ £¬²¢¼ò»¯ÁË×ðÁú¿­Ê±´úÂë¡£

ÒÔÉϾÍÊÇlaravelÔõô·µ»ØÖ¸¶¨ÁеÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿