尊龙凯时人生就是搏

laravelÊý¾Ý¿âÉèÖÃ

laravelÊÇÒ»¿î¿ªÔ´µÄphp web¿ò¼Ü£¬ËüÔÚ´¦ÀíÊý¾Ý·½ÃæºÜÊǾ«²Ê¡£laravelÌṩÁËÒ»ÖÖ¾«Á·ÎÞа¡¢Ò×ÓÚʹÓõÄorm(object relational mapping)·½·¨£¬ÉÁ¿ª·¢ÕßÃÇÔÚ´¦Àí²î±ðµÄÊý¾Ý¿âʱԽ·¢Àû±ã¡£

ÔÚʹÓÃLaravelʱ£¬ÎÒÃÇÐèÒªÏȾÙÐÐÊý¾Ý¿âÁ´½ÓÉèÖã¬ÈÃLaravelÄܹ»×¼È·»á¼ûÎÒÃǵÄÊý¾Ý¿â¡£ÏÂÃæÎÒÃǽ«½â˵ÔõÑùÔÚLaravelÖоÙÐÐÊý¾Ý¿âÉèÖá£

1. ÇéÐαäÁ¿

ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÐÞ¸Ä.envÎļþÀ´ÉèÖÃÎÒÃǵÄÊý¾Ý¿âÐÅÏ¢¡£ÎÒÃÇ¿ÉÒÔÔÚ.envÎļþÖÐÕÒµ½ÈçÏÂÐÅÏ¢£º

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

µÇ¼ºó¸´ÖÆ

DB_CONNECTION ÓÃÓÚÖ¸¶¨Ãü¾Ý¿âµÄÀàÐÍ£¬LaravelÖ§³ÖµÄÊý¾Ý¿âÀàÐÍÓÐmysql¡¢pgsql¡¢sqlite¡¢sqlsrvµÈ¡£

DB_HOST ÓÃÓÚÖ¸¶¨Ãü¾Ý¿âËùÔÚµÄÖ÷»úÃû»òIPµØµã£¬Ò»Ñùƽ³£Ö¸¶¨Îªlocalhost»ò127.0.0.1¡£

DB_PORT ÓÃÓÚÖ¸¶¨Ãü¾Ý¿âЧÀÍÆ÷µÄ¶Ë¿ÚºÅ¡£

DB_DATABASE ÓÃÓÚÖ¸¶¨ÒªÊ¹ÓõÄÊý¾Ý¿âÃû³Æ¡£

DB_USERNAME ÓÃÓÚÖ¸¶¨ÅþÁ¬Êý¾Ý¿âʹÓõÄÓû§Ãû¡£

DB_PASSWORD ÓÃÓÚÖ¸¶¨ÅþÁ¬Êý¾Ý¿âʹÓõÄÃÜÂë¡£

ÔÚ¾ÙÐÐÍêÒÔÉϵÄÉèÖÃÖ®ºó£¬Laravel½«»áʹÓÃÕâЩÉèÖÃÀ´ÅþÁ¬ÎÒÃǵÄÊý¾Ý¿â¡£

2. Êý¾Ý¿âǨáã

LaravelÌṩÁËÊý¾Ý¿âǨáãµÄ¹¦Ð§£¬¿ÉÒÔÀû±ãÎÒÃÇÔÚ²î±ðµÄÊý¾Ý¿âÖ®¼ä¾ÙÐÐÊý¾ÝǨáã¡£ÔÚ¾ÙÐÐÊý¾Ý¿âǨáãʱÐèҪעÖصÄÊÇ£¬ÎÒÃÇÐèÒªÏȽ¨ÉèÊý¾Ý¿â²¢ÉèÖúÃÏìÓ¦µÄÅþÁ¬ÐÅÏ¢£¬È»ºóÔÙʹÓÃǨáãÆ÷¾ÙÐÐÊý¾ÝǨáã¡£

ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÖ´ÐÐphp artisan make:migration create_users_tableÏÂÁîÀ´½¨ÉèÒ»¸öǨáãÎļþ¡£¸ÃÏÂÁ»áÔÚdatabase/migrationsĿ¼ÖÐÌìÉúÒ»¸öеÄǨáãÎļþ£¬ÎļþÃûÀàËÆÓÚ2019_04_01_000001_create_users_table.php¡£

ÔÚ½¨ÉèÍêǨáãÎļþÖ®ºó£¬ÎÒÃÇÐèÒª·­¿ª¸ÃÎļþ²¢±à¼­ÆäÖеÄupÒªÁìºÍdownÒªÁì¡£ÆäÖУ¬upÒªÁ콫»áÔÚÖ´ÐÐǨáãʱ±»Å²Óã¬ÓÃÓÚ½ç˵ÎÒÃÇÐèÒªÖ´ÐеÄÊý¾Ý¿â²Ù×÷£»downÒªÁ콫»áÔÚ×÷·ÏǨáãʱ±»Å²Óã¬ÓÃÓÚ½ç˵ÎÒÃÇÐèÒªÖ´ÐеÄ×÷·Ï²Ù×÷¡£ÏÂÃæÎÒÃÇÒÔ½¨ÉèÓû§±íΪÀý£¬¾ÙÐдúÂëÊ÷Ä££º

<?php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂ뽫»á½¨ÉèÒ»¸öÃûΪusersµÄ±í£¬¸Ã±í°üÀ¨5¸ö×Ö¶Îid¡¢name¡¢email¡¢passwordºÍremember_token£¬ÒÔ¼°Á½¸ö×Ô¶¯Î¬»¤µÄ×Ö¶Îcreated_atºÍupdated_at¡£

ÔÚÍê³ÉÁËÉÏÊöÉèÖÃÖ®ºó£¬ÎÒÃÇ¿ÉÒÔÖ´ÐÐphp artisan migrateÏÂÁîÀ´Ö´ÐÐÊý¾ÝǨáã²Ù×÷¡£

3. Ä£×Ó

ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃEloquent ORMÀ´Àû±ãµØ²Ù×÷ÎÒÃǵÄÊý¾Ý¿â¡£Eloquent ORMÌṩÁËÐí¶àÒªÁìÓÃÓÚÖ´ÐÐCRUD(create, read, update, delete)²Ù×÷£¬Äܹ»×ÊÖúÎÒÃÇ¿ìËÙ¾ÙÐÐÊý¾Ý¿â²Ù×÷¡£

ÎÒÃÇÏÈÀ´¿´Ò»ÏÂÔÚÄ£×ÓÖÐÉèÖÃÊý¾Ý¿âµÄÒªÁì¡£ÔÚÄ£×ÓÀàÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÒªÁìÀ´Ö¸¶¨±íÃû¡¢Ö÷¼üÒÔ¼°Êý¾Ý¿âÅþÁ¬ÐÅÏ¢£º

<?php

namespace AppModels;

use IlluminateDatabaseEloquentModel;

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

    protected $primaryKey = 'id';

    protected $connection = 'mysql';
}

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂ뽫»áÖ¸¶¨Ê¹ÓÃmysqlÅþÁ¬À´»á¼ûusers±í£¬¸Ã±íµÄÖ÷¼üΪid¡£

ÔÚÉèÖÃÍêÊý¾Ý¿âÅþÁ¬ÐÅÏ¢ºó£¬ÎÒÃǾͿÉÒÔʹÓÃEloquent ORMÀ´¾ÙÐÐÊý¾Ý¿â²Ù×÷ÁË¡£ÏÂÃæÎÒÃÇÀ´¿´Ò»Ð©Eloquent ORMµÄ»ù±¾²Ù×÷¡£

3.1 ½¨ÉèÊý¾Ý

ÔÚEloquent ORMÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃcreateÒªÁìÀ´½¨ÉèÊý¾Ý¡£ÀýÈ磺

$user = User::create([
    'name' => 'Tom',
    'email' => 'tom@example.com',
    'password' => bcrypt('password'),
]);

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂ뽫»á½¨ÉèÒ»ÌõÃûΪTom¡¢ÓÊÏäΪtom@example.com¡¢ÃÜÂëΪpasswordµÄÓû§Êý¾Ý¡£

3.2 ÅÌÎÊÊý¾Ý

ÔÚEloquent ORMÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃgetÒªÁìÀ´ÅÌÎÊÊý¾Ý¡£ÀýÈ磺

$users = User::get();

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂ뽫»á´Óusers±íÖÐÅÌÎʳöËùÓеÄÓû§Êý¾Ý¡£

ÎÒÃÇÒ²¿ÉÒÔʹÓÃwhereÒªÁìÀ´¾ÙÐÐÌõ¼þÅÌÎÊ¡£ÀýÈ磺

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

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂ뽫»á´Óusers±íÖÐÅÌÎÊ×ÅÃû³ÆΪTomµÄËùÓÐÓû§Êý¾Ý¡£

3.3 ¸üÐÂÊý¾Ý

ÔÚEloquent ORMÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃupdateÒªÁìÀ´¸üÐÂÊý¾Ý¡£ÀýÈ磺

$user = User::where('name', 'Tom')->first();
$user->email = 'new_email@example.com';
$user->save();

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂ뽫»á½«ÃûΪTomµÄÓû§Êý¾ÝµÄÓÊÏä¸ÄΪnew_email@example.com¡£

3.4 ɾ³ýÊý¾Ý

ÔÚEloquent ORMÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃdeleteÒªÁìÀ´É¾³ýÊý¾Ý¡£ÀýÈ磺

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

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂ뽫»áɾ³ýÃûΪTomµÄÓû§Êý¾Ý¡£

½áÓï

×ÜÖ®£¬LaravelÌṩÁ˸»ºñµÄÊý¾Ý¿â²Ù×÷ÒªÁ죬¿ÉÒÔÈÃÎÒÃÇÔÚ¿ª·¢WebÓ¦ÓÃʱԽ·¢±ã½Ý¡£ÔÚ¾ÙÐÐÊý¾Ý¿âÉèÖÃʱ£¬ÎÒÃÇÐèҪעÖØÇéÐαäÁ¿µÄÉèÖÃÒÔ¼°Êý¾Ý¿âǨáãÎļþµÄ±à¼­£¬×èÖ¹·ºÆð²»ÐëÒªµÄ¹ýʧ¡£Í¬Ê±£¬Eloquent ORMҲΪÎÒÃÇÌṩÁËÀû±ã¿ì½ÝµÄCRUD²Ù×÷ÒªÁ죬¿ÉÒÔÈÃÎÒÃÇÔÚ¿ª·¢Àú³ÌÖÐÔ½·¢¸ßЧ¡£

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

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

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

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

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

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