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

laravelÔõÑù²Ù×÷Êý¾Ý¿â

laravelÊÇÒ»¿îºÜÊÇÓÅÒìµÄphp¿ò¼Ü £¬Îª¿ª·¢Ö°Ô±ÌṩÁËÐí¶à±ãµ± £¬Èç¼ò»¯±àÂë¡¢Ìá¸ß¿ª·¢Ð§ÂʺÍÒ×ÓÚά»¤µÈ¡£Óë´Ëͬʱ £¬laravelÒ²Ö§³Ö¶àÖÖÊý¾Ý¿â²Ù×÷ÒªÁì £¬Ê¹¿ª·¢Ö°Ô±Ô½·¢±ã½ÝµØ¾ÙÐÐÊý¾Ý¿â²Ù×÷¡£ÔÚ±¾ÆªÎÄÕÂÖÐ £¬ÎÒÃǽ«ÏÈÈÝlaravel¿ò¼ÜÖÐÔõÑù¾ÙÐÐÊý¾Ý¿â²Ù×÷¡£

µÚÒ»²½£ºÉèÖÃÊý¾Ý¿âÐÅÏ¢

Ê×ÏÈ £¬ÎÒÃÇÐèÒªÔÚLaravelÏîÄ¿ÖÐÉèÖÃÊý¾Ý¿âÐÅÏ¢¡£¿ÉÒÔÔÚconfig/database.phpÎļþÖоÙÐÐÉèÖà £¬ÆäÖаüÀ¨Êý¾Ý¿âÀàÐÍ¡¢Ö÷»úµØµã¡¢¶Ë¿Ú¡¢Êý¾Ý¿âÃû¡¢Óû§ÃûºÍÃÜÂëµÈÐÅÏ¢¡£

ÔÚLaravelÖÐ £¬¿ÉÒÔÖ§³Ö¶à¸öÊý¾Ý¿â¡£Òò´Ë £¬ÐèÒªÔÚconfig/database.php ÖоÙÐÐÉèÖà £¬ÀýÈ磺

'connections' => [

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

    'mysql2' => [
        'driver' => 'mysql',
        'host' => env('DB2_HOST', '127.0.0.1'),
        'port' => env('DB2_PORT', '3306'),
        'database' => env('DB2_DATABASE', 'forge'),
        'username' => env('DB2_USERNAME', 'forge'),
        'password' => env('DB2_PASSWORD', ''),
        'unix_socket' => env('DB2_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

µÇ¼ºó¸´ÖÆ

¿ÉÒÔ¿´µ½ £¬ÕâÀï½ç˵ÁËÁ½¸öÊý¾Ý¿âÅþÁ¬£ºmysqlºÍmysql2 £¬ËüÃÇ»®·Ö¶ÔÓ¦²î±ðµÄÊý¾Ý¿â¡£

ÔÚ.envÎļþÖпÉÒÔÖ¸¶¨Ã¿¸öÊý¾Ý¿âµÄÏà¹ØÉèÖãº

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_username
DB_PASSWORD=database_password

DB_CONNECTION=mysql2
DB_HOST=127.0.0.1
DB_PORT=3306
DB2_DATABASE=database2_name
DB2_USERNAME=database2_username
DB2_PASSWORD=database2_password

µÇ¼ºó¸´ÖÆ

ÕâЩÉèÖÃÐÅÏ¢½«ÔÚLaravelÓ¦ÓóÌÐòÖÐʹÓá£

µÚ¶þ²½£º½¨ÉèÄ£×Ó

ÔÚLaravel¿ò¼ÜÖÐ £¬Ä£×Ó¿ÉÓÃÓÚÓëÊý¾Ý¿â½»»¥¡£Ä£×ÓÊÇÒ»¸öÀà £¬ÓÃÓÚ´¦ÀíÊý¾Ý¿âÖеÄÊý¾Ý¡£Ê¹ÓÃÄ£×Ó¿ÉÒÔ°ÑÊý¾Ý¿âµÄ±íÓ³Éäµ½Ò»¸öPHPÀàÖÐ £¬Ê¹ÎÒÃÇÄܹ»¸üÀû±ãµØ²Ù×÷Êý¾Ý¿â¡£

ʹÓÃArtisanÏÂÁîÐй¤¾ßÀ´¿ìËÙÌìÉúÄ£×Ó £¬ÀýÈ磺

php artisan make:model User

µÇ¼ºó¸´ÖÆ

Õ⽫ÔÚapp/ModelsĿ¼ÏÂÌìÉúÒ»¸öÃûΪUserµÄÄ£×ÓÀà £¬¿ÉÒÔÔÚÆäÖнç˵±í¡¢Ö÷¼ü¡¢Ê±¼ä´ÁµÈÐÅÏ¢¡£

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = 'user';
    protected $primaryKey = 'id';
    public $timestamps = true;
}

µÇ¼ºó¸´ÖÆ

µÚÈý²½£ºÅÌÎÊÊý¾Ý

ÔÚLaravelÖÐ £¬Í¨¹ýEloquent ORM¿ÉÒÔÀû±ãµØ¾ÙÐÐÊý¾Ý¿âÅÌÎÊ¡£Í¨¹ýÄ£×Ó¿ÉÒÔÇáËɵØÅÌÎÊij¸ö±í¡£ÀýÈçʹÓÃÒÔÏÂEloquentÒªÁì¿ÉÒÔ»ñÈ¡¡°User¡±±íÖеÄËùÓмͼ£º

use App\Models\User;

$users = User::all();

µÇ¼ºó¸´ÖÆ

ʹÓÃfindOrFailÒªÁì²éÕҼͼʱ £¬¿ÉÒÔ½«¸ÃÒªÁìÓëÄ£×ÓIDÒ»ÆðʹÓãº

$user = User::findOrFail(1);

µÇ¼ºó¸´ÖÆ

ÅÌÎÊijЩ¼Í¼ʱ £¬¿ÉÒÔʹÓÃwhereÒªÁ죺

use App\Models\User;

$users = User::where('name', 'John')->get();

µÇ¼ºó¸´ÖÆ

»¹¿ÉÒÔʹÓÃÆäËûÌõ¼þ¾ÙÐÐÅÌÎÊ £¬ÀýÈ磺

$user = User::where('name', 'John')->first();

µÇ¼ºó¸´ÖÆ

$users = User::select('name')->distinct()->get();

µÇ¼ºó¸´ÖÆ

$users = User::orderBy('name', 'desc')->take(10)->get();

µÇ¼ºó¸´ÖÆ

µÚËIJ½£º²åÈëÊý¾Ý

ÔÚLaravel¿ò¼ÜÖвåÈëÊý¾ÝºÜÊǼòÆÓ¡£Ê¹ÓÃÄ£×Ó½¨Éèмͼʱ £¬¿ÉÒÔʹÓÃÒÔÏ´úÂ룺

$user = new User;
$user->name = 'John';
$user->email = 'john@example.com';
$user->save();

µÇ¼ºó¸´ÖÆ

»¹¿ÉÒÔÔÚÒ»¸öÀàÖÐʹÓÃcreateÒªÁìÒ»´Î½¨Éè¶à¸öмͼ£º

User::create([
    'name' => 'Jane',
    'email' => 'jane@example.com',
]);

µÇ¼ºó¸´ÖÆ

µÚÎå²½£º¸üÐÂÊý¾Ý

ͨ¹ýÄ£×Ó¸üÐÂÊý¾ÝÒ²ºÜÊÇÈÝÒס£¿ÉÒÔͨ¹ýÒÔÏ´úÂë¸üÐÂÒ»¸ö¼Í¼£º

$user = User::find(1);

$user->email = 'john.doe@example.com';

$user->save();

µÇ¼ºó¸´ÖÆ

Ò²¿ÉÒÔÅúÁ¿¸üмͼ£º

User::where('name', 'John')->update(['email' => 'john.doe@example.com']);

µÇ¼ºó¸´ÖÆ

µÚÁù²½£ºÉ¾³ýÊý¾Ý

ɾ³ýÊý¾ÝÓë¸üÐÂÊý¾ÝµÄÒªÁìÀàËÆ¡£ÈôÊÇÄúҪɾ³ýµ¥¸ö¼Í¼ £¬ÇëʹÓÃÒÔÏÂÏÂÁ

$user = User::find(1);

$user->delete();

µÇ¼ºó¸´ÖÆ

µ±ÐèҪɾ³ý¶à¸ö¼Í¼ʱ £¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ

User::where('name', 'John')->delete();

µÇ¼ºó¸´ÖÆ

×¢ÖØ £¬deleteÒªÌå»á½«¼Í¼´ÓÊý¾Ý¿âÖÐÓÀÊÀɾ³ý¡£¿ÉÒÔʹÓÃÈíɾ³ý¹¦Ð§½«ÆäÒƶ¯µ½ÁíÒ»¸ö±íÖÐ £¬¶ø²»ÊÇÓÀÊÀɾ³ý¡£

×ܽá

Laravel ORMΪ³ÌÐòÔ±ÌṩÁËÒ»¸öÇ¿Ê¢µÄ¹¤¾ßÀ´²Ù×÷Êý¾Ý¿â¡£Í¨¹ýÉÏÊö¼¸¸ö°ì·¨ £¬Äú¿ÉÒÔÇáËɵؾÙÐÐÊý¾Ý¿âÅÌÎÊ¡¢²åÈë¡¢¸üкÍɾ³ýµÈ²Ù×÷¡£ÖµµÃ×¢ÖصÄÊÇ £¬Ó¦¸ÃºÜÊÇСÐĵش¦ÀíÃô¸ÐÊý¾Ý²¢ÇÒ¼á³ÖÓÅÒìµÄÐÐΪ¡£Ï£ÍûÕâƪÎÄÕ¶ÔÄúÔÚLaravelÖвÙ×÷Êý¾Ý¿âÓÐËù×ÊÖú¡£

ÒÔÉϾÍÊÇlaravelÔõÑù²Ù×÷Êý¾Ý¿âµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

QR code
ÍøÕ¾µØͼ