×ðÁú¿­Ê±¹ÙÍøµÇ¼

laravel»ñÈ¡ÒªÁì

laravel ÊÇÒ»¿îÏÖ´ú»¯µÄ php ¿ò¼Ü£¬ËüÒÔ¾«Á·µÄÓï·¨ºÍÇ¿Ê¢µÄ¹¦Ð§¶ø±»ÆÕ±éÓ¦Óá£ÔÚ laravel ÖлñÈ¡Êý¾ÝºÍÒªÁìÊǺÜÊÇÖ÷ÒªµÄÒ»¸öÖ÷Ì⣬ÓÉÓÚÔÚ¿ª·¢Àú³ÌÖУ¬ÎÒÃÇÐèÒª¾­³£´ÓÊý¾Ý¿â»òÆäËûЧÀÍÖлñÈ¡Êý¾ÝÀ´Íê³ÉÓªÒµÂß¼­¡£

±¾ÎĽ«ÏÈÈÝ Laravel ÖлñÈ¡ÒªÁìµÄÏà¹Ø֪ʶ£¬°üÀ¨ÅÌÎʽṹÆ÷¡¢ORM£¨¹¤¾ß¹ØϵӳÉ䣩¡¢Eloquent Ä£×Ó¡¢¹Øϵģ×ӵȡ£

Ò»¡¢ÅÌÎʽṹÆ÷

ÅÌÎʽṹÆ÷ÊÇ Laravel ÌṩµÄÒ»ÖÖÀû±ãµÄÊý¾Ý¿âÅÌÎʹ¤¾ß£¬¿ÉÒÔ×ÊÖúÎÒÃÇ»ñÈ¡Êý¾Ý²¢¾ÙÐÐÖÖÖÖÊý¾Ý²Ù×÷¡£Ê¹ÓÃÅÌÎʽṹÆ÷£¬ÎÒÃÇ¿ÉÒÔÀû±ãµØÅÌÎÊÊý¾Ý£¬²¢¶ÔÅÌÎÊЧ¹û¾ÙÐÐÅÅÐò¡¢·Ö×é¡¢¾ÛºÏµÈ²Ù×÷¡£

ÀýÈ磬ÔÚ´ÓÊý¾Ý¿âÖÐÅÌÎÊÓû§Êý¾Ýʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂ룺

$users = DB::table('users')->get();

µÇ¼ºó¸´ÖÆ

ÕâÐдúÂ뽫´ÓÃûΪ ¡°users¡± µÄÊý¾Ý±íÖлñÈ¡ËùÓÐÓû§Êý¾Ý¡£ÎÒÃÇ»¹¿ÉÒÔÌí¼ÓÆäËüÌõ¼þÀ´»ñÈ¡Ìض¨µÄÊý¾Ý¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏ´úÂë»ñÈ¡ËùÓÐÄêËê´óÓÚ 18 µÄÓû§£º

$users = DB::table('users')->where('age', '>', 18)->get();

µÇ¼ºó¸´ÖÆ

ͨ¹ýÅÌÎʽṹÆ÷£¬ÎÒÃÇ¿ÉÒÔʹÓÃÐí¶àº¯Êý¾ÙÐÐÖØ´óÅÌÎÊ¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓà groupBy ÒªÁ콫Ч¹ûƾ֤Ìض¨ÁзÖ×飺

$users = DB::table('users')
            ->groupBy('account_id')
            ->having('account_id', '>', 100)
            ->get();

µÇ¼ºó¸´ÖÆ

¶þ¡¢ORM

ORM£¨¹¤¾ß¹ØϵӳÉ䣩ÊÇÒ»ÖÖ½«Êý¾Ý¿âÖеÄÊý¾ÝÓ³Éäµ½¹¤¾ßµÄÊÖÒÕ¡£Laravel µÄ ORM »ùÓÚ Eloquent Ä£×ÓʵÏÖ£¬¿ÉÒÔ½«Êý¾Ý±íÖеÄÊý¾ÝÓ³Éäµ½ PHP ¹¤¾ß»òÊý×éÖУ¬´Ó¶øʹÎÒÃÇÄܹ»Àû±ãµØ¾ÙÐÐÊý¾Ý²Ù×÷¡£

ÀýÈ磬ÔÚʹÓà ORM ʱ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏ´úÂë»ñÈ¡Óû§Êý¾Ý£º

$users = AppUser::all();

µÇ¼ºó¸´ÖÆ

ÕâÐдúÂ뽫ʹÓà Eloquent Ä£×Ó´ÓÓû§Êý¾Ý±íÖлñÈ¡ËùÓÐÓû§Êý¾Ý£¬²¢½«ÆäÓ³Éäµ½ User ¹¤¾ßÖС£ÎÒÃÇ»¹¿ÉÒÔÌí¼ÓÆäËüÌõ¼þÀ´»ñÈ¡Ìض¨µÄÊý¾Ý¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓà where ÒªÁì»ñÈ¡ËùÓÐÄêËê´óÓÚ 18 µÄÓû§£º

$users = AppUser::where('age', '>', 18)
                ->get();

µÇ¼ºó¸´ÖÆ

Èý¡¢Eloquent Ä£×Ó

Eloquent Ä£×ÓÊÇ Laravel ÖеÄÒ»¸ö½¹µã¿´·¨£¬ËüΪÎÒÃÇÌṩÁË»á¼ûÊý¾Ý¿âµÄÒªÁìºÍÊôÐÔ¡£ÔÚʹÓà Eloquent Ä£×Óʱ£¬ÎÒÃÇÐèÒª½ç˵һ¸öÄ£×ÓÀ࣬²¢½«ÆäÓ³Éäµ½Êý¾Ý¿âÖеÄÊý¾Ý±íÉÏ¡£È»ºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃÄ£×ÓʵÀýÀ´»á¼ûÊý¾Ý¿âÖеÄÊý¾Ý¡£

ÀýÈ磬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏ´úÂ뽨ÉèÒ»¸ö User Ä£×ÓÀࣺ

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class User extends Model
{
    protected $table = 'users';
}

µÇ¼ºó¸´ÖÆ

ÔÚÕâ¸öÄ£×ÓÀàÖУ¬ÎÒÃÇÖ¸¶¨ÁËÊý¾Ý±íΪ ¡°users¡±£¬ÕâÑù Laravel ¾ÍÖªµÀÁËÎÒÃÇÒª½«Õâ¸öÄ£×ÓÓëÄĸöÊý¾Ý±í¹ØÁª¡£È»ºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´»ñÈ¡Óû§Êý¾Ý£º

$users = User::all();

µÇ¼ºó¸´ÖÆ

ÕâÐдúÂ뽫´Ó User Ä£×Ó¶ÔÓ¦µÄÊý¾Ý±íÖлñÈ¡ËùÓÐÓû§Êý¾Ý£¬²¢½«ÆäÓ³Éäµ½ User ¹¤¾ßÖС£ÎÒÃÇ»¹¿ÉÒÔÌí¼ÓÆäËûÌõ¼þÀ´»ñÈ¡Ìض¨µÄÊý¾Ý¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓà where ÒªÁì»ñÈ¡ËùÓÐÄêËê´óÓÚ 18 µÄÓû§£º

$users = User::where('age', '>', 18)->get();

µÇ¼ºó¸´ÖÆ

ËÄ¡¢¹Øϵģ×Ó

ÔÚÐí¶àÓ¦ÓóÌÐòÖУ¬Êý¾ÝÖ®¼äÍùÍù±£´æ¹ØÁª¹Øϵ£¨ÈçÒ»¶Ô¶à¡¢¶à¶Ô¶àµÈ£©¡£Laravel ÖÐµÄ Eloquent Ä£×Ó¿ÉÒÔÀû±ãµØ´¦ÀíÕâЩ¹ØÁª¹Øϵ£¬Í¨¹ý¹Øϵģ×Ó£¬ÎÒÃÇ¿ÉÒÔÇáËɵػñÈ¡Êý¾Ý¿âÖÐÏà¹ØÁªµÄÊý¾Ý¡£

ÀýÈ磬ÔÚÒ»¸ö²©¿ÍÓ¦ÓóÌÐòÖУ¬ÎÒÃÇ¿ÉÄÜÓÐÒ»¸ö Post Ä£×ÓºÍÒ»¸ö Comment Ä£×Ó¡£Ò»¸ö Post ¿ÉÄÜÓжà¸ö Comment£¬Òò´ËÎÒÃÇÐèÒª½¨ÉèÒ»¸öÒ»¶Ô¶à¹Øϵ¡£ÎÒÃÇ¿ÉÒÔÔÚ Post Ä£×ÓÖÐʹÓÃÒÔÏ´úÂë½çËÃ÷ÈÕâ¸ö¹Øϵ£º

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Post extends Model
{
    protected $table = 'posts';

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÕâ¸ö´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸ö comments ÒªÁ죬¸ÃÒªÁì·µ»ØÕâ¸ö Post ´øÓеÄËùÓÐ Comment¡£ÔÚ Comment Ä£×ÓÖУ¬ÎÒÃÇ»¹ÐèÒª½ç˵һ¸öÒªÁìÀ´Ö¸¶¨ Comment ÊôÓÚÄĸö Post£º

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Comment extends Model
{
    protected $table = 'comments';

    public function post()
    {
        return $this->belongsTo(Post::class);
    }
}

µÇ¼ºó¸´ÖÆ

ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏ´úÂë»ñȡijƪÎÄÕµÄ̸ÂÛ£º

$post = Post::find(1);

$comments = $post->comments;

µÇ¼ºó¸´ÖÆ

ÕâÐдúÂ뽫·µ»Ø id Ϊ 1 µÄ Post ´øÓеÄËùÓÐ Comment¡£

×ܽá

ÒÔÉÏÊÇ Laravel ÖлñÈ¡ÒªÁìµÄÏà¹Ø֪ʶ¡£ÅÌÎʽṹÆ÷¡¢ORM¡¢Eloquent Ä£×Ӻ͹Øϵģ×Ó£¬ÕâЩǿʢµÄ¹¦Ð§ÎªÎÒÃÇÔÚ¿ª·¢ÖлñÈ¡ºÍ´¦ÀíÊý¾ÝÌṩÁ˱ã½ÝµÄÒªÁì¡£ÎÞÂÛÊÇ´ÓÊý¾Ý¿â¡¢»º´æÕվɴÓÆäËûЧÀÍÖлñÈ¡Êý¾Ý£¬ÔÚ Laravel Öж¼ÓÐÐí¶àÀû±ãµÄ²½·¥¡£Í¨¹ýÕÆÎÕÕâЩ֪ʶ£¬ÎÒÃÇ¿ÉÒÔ¸üÇáËɵØʵÏÖÖØ´óµÄÓªÒµÂß¼­¡£

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

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

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

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

QR code
ÍøÕ¾µØͼ