×ðÁú¿­Ê±

Laravel¿ª·¢£ºÔõÑùʹÓÃÄ£×Ó¹¤³§²âÊÔÊý¾Ý¿â £¿

laravelÊÇÒ»¸öÊ¢ÐеÄphp web¿ª·¢¿ò¼Ü £¬ÒÔÆ侫Á·Ò×ÓõÄapiÉè¼Æ £¬¸»ºñµÄº¯Êý¿âºÍÇ¿Ê¢µÄÉú̬ϵͳ¶øÖøÃû¡£ÔÚʹÓÃlaravel¾ÙÐÐÏîÄ¿¿ª·¢Ê± £¬²âÊÔÊǺÜÊÇÖ÷ÒªµÄÒ»¸ö»·½Ú¡£laravelÌṩÁ˶àÖÖ²âÊÔ¹¤¾ßºÍÊÖÒÕ £¬ÆäÖÐÄ£×Ó¹¤³§ÊÇÆäÖеÄÖ÷Òª×é³É²¿·Ö¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚlaravelÏîÄ¿ÖÐʹÓÃÄ£×Ó¹¤³§À´²âÊÔÊý¾Ý¿â¡£

Ò»¡¢Ä£×Ó¹¤³§µÄ×÷ÓÃ

ÔÚLaravelÖÐ £¬Ä£×Ó¹¤³§ÊÇÓÃÀ´ÌìÉú²âÊÔÊý¾ÝµÄ¹¤¾ß¡£ÎÒÃÇͨ³£Ê¹ÓÃÄ£×Ó¹¤³§À´½¨ÉèÄ£×ÓʵÀý £¬È»ºó½«Æä´æÈëÊý¾Ý¿âÖС£ÕâЩģ×ÓʵÀý¿ÉÒÔ±»ÓÃÓÚ²âÊÔÄ£×ÓµÄÊôÐÔ¡¢¹ØϵµÈ¡£Ä£×Ó¹¤³§µÄÖ÷Òª×÷ÓÃÈçÏ£º

1¡¢Àû±ãµØÌìÉú²âÊÔÊý¾Ý£ºÄ£×Ó¹¤³§¿ÉÒÔÔÚ¼¸ÐдúÂëÄÚÌìÉú´ó×Ú²âÊÔÊý¾Ý £¬ÕâÈÃÎÒÃÇÄܹ»¿ìËٵع¹½¨²âÊÔÓÃÀý¡£

2¡¢ïÔÌ­´úÂëÈßÓࣺÔÚÿ´Î²âÊÔÖÐÊÖ¶¯±àд²âÊÔÊý¾Ý»áÔöÌí´úÂëÈßÓà £¬¶øʹÓÃÄ£×Ó¹¤³§Ôò¿ÉÒÔïÔÌ­ÕâÖÖÈßÓà¡£

3¡¢Ìá¸ß²âÊԵĿɿ¿ÐÔ£ºÊ¹ÓÃÄ£×Ó¹¤³§¿ÉÒÔÈ·±£²âÊÔÓÃÀý¾ßÓпÉÖظ´ÐÔ £¬´Ó¶øÌá¸ß²âÊԵĿɿ¿ÐÔ¡£

¶þ¡¢Ê¹ÓÃÄ£×Ó¹¤³§²âÊÔÊý¾Ý¿â

½ÓÏÂÀ´ÎÒÃǽ«Í¨¹ýÒ»¸öÏÖʵµÄÀý×ÓÀ´ÑÝʾÔõÑùʹÓÃÄ£×Ó¹¤³§À´²âÊÔÊý¾Ý¿â¡£

¼ÙÉèÎÒÃÇÓÐÒ»¸öÓû§Ä£×Ó £¬ÆäÖаüÀ¨nameºÍemailÁ½¸öÊôÐÔ¡£ÎÒÃÇÐèÒª±àдһ¸ö²âÊÔÓÃÀýÀ´²âÊÔÓû§Ä£×ӵĴ´Á¢¹¦Ð§¡£

1¡¢½¨Éè²âÊÔÎļþ

ÔÚLaravelÏîÄ¿ÖÐ £¬ÎÒÃÇ¿ÉÒÔʹÓÃartisanÏÂÁîÀ´ÌìÉú²âÊÔÎļþ¡£·­¿ªÖÕ¶Ë £¬½øÈëÏîÄ¿¸ùĿ¼²¢ÔËÐÐÒÔÏÂÏÂÁ

php artisan make:test UserModelTest

µÇ¼ºó¸´ÖÆ

¸ÃÏÂÁÔÚtests/UnitĿ¼ÏÂÌìÉúÒ»¸öÃûΪUserModelTest.phpµÄ²âÊÔÎļþ¡£

2¡¢±àд²âÊÔÒªÁì

·­¿ªUserModelTest.phpÎļþ £¬ÎÒÃÇ¿ÉÒÔ¿´µ½ÒÔÏÂĬÈϵIJâÊÔÒªÁ죺

public function test_example()
{
    $this->assertTrue(true);
}

µÇ¼ºó¸´ÖÆ

½«ÆäÐÞ¸ÄΪÏÂÃæµÄ´úÂ룺

public function test_create_user()
{
    $user = factory(User::class)->create([
        'name' => 'Tom',
        'email' => 'tom@example.com',
    ]);

    $this->assertInstanceOf(User::class, $user);
    $this->assertEquals('Tom', $user->name);
    $this->assertEquals('tom@example.com', $user->email);
}

µÇ¼ºó¸´ÖÆ

´úÂëÚ¹ÊÍ£º

1£©Ê¹ÓÃfactoryÒªÁ콨ÉèÒ»¸öUserÄ£×ÓʵÀý¡£

2£©ÉèÖÃnameºÍemailÊôÐÔµÄÖµ¡£

3£©Ê¹ÓÃcreateÒªÁ콫¸ÃÄ£×ÓʵÀý´æ´¢µ½Êý¾Ý¿â¡£

4£©¶ÏÑÔ¸ÃÄ£×ÓʵÀýÊÇUserÀàÐÍ¡£

5£©¶ÏÑÔ¸ÃÄ£×ÓʵÀýµÄnameÊôÐÔֵΪ’Tom’¡£

6£©¶ÏÑÔ¸ÃÄ£×ÓʵÀýµÄemailÊôÐÔֵΪ’tom@example.com’¡£

3¡¢ÔËÐвâÊÔ

ÉúÑÄÎļþ²¢·­¿ªÖÕ¶Ë £¬ÔËÐÐÒÔÏÂÏÂÁî×îÏȲâÊÔ£º

php artisan test

µÇ¼ºó¸´ÖÆ

¸ÃÏÂÁִÐÐÏîÄ¿Öб£´æµÄËùÓвâÊÔÓÃÀý£¨ËùÓÐÎļþÃûºó׺ΪTest.phpµÄÎļþ£©¡£

ÈôÊDzâÊÔͨ¹ý £¬ÎÒÃǻῴµ½ÒÔÏÂÊä³ö£º

   PASS  TestsUnitUserTest
   create user

  Tests:  1 passed
  Time:   0.10s

µÇ¼ºó¸´ÖÆ

ͨ¹ýÒÔÉÏ°ì·¨ £¬ÎÒÃÇÀֳɵØʹÓÃÄ£×Ó¹¤³§²âÊÔÁËÓû§Ä£×ӵĴ´Á¢¹¦Ð§¡£ÔÚÏÖʵÏîÄ¿ÖÐ £¬ÎÒÃÇ¿ÉÒÔƾ֤ÀàËƵķ½·¨±àдÖݪֲâÊÔÓÃÀýÀ´²âÊÔÄ£×ӵIJî±ð¹¦Ð§¡£

Èý¡¢Ä£×Ó¹¤³§µÄ¸ß¼¶Ó÷¨

³ýÁËÉÏÎÄËùÊöµÄ»ù±¾Ê¹Ó÷½·¨Íâ £¬Ä£×Ó¹¤³§»¹¾ßÓÐÒÔϸ߼¶Ó÷¨£º

1¡¢½ç˵ģ×Ó¹¤³§

Laravelͨ¹ýFaker¿âÀ´ÌìÉúÄ£×ÓʵÀýµÄÊôÐÔÖµ¡£FakerÊÇÒ»¸öPHP¿â £¬ÓÃÓÚÌìÉú¼ÙµÄÊý¾Ý¡£ÎÒÃÇ¿ÉÒÔ×Ô½ç˵ģ×Ó¹¤³§À´½ç˵ģ×ÓʵÀýµÄÊôÐÔÖµ¡£·­¿ªÊý¾Ý¿â¹¤³§Îļþ£¨database/factories/ModelFactory.php£© £¬¿ÉÒÔ¿´µ½ÒÔÏ´úÂ룺

$factory->define(AppUser::class, function (FakerGenerator $faker) {
    static $password;

    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'password' => $password ?: $password = bcrypt('secret'),
        'remember_token' => str_random(10),
    ];
});

µÇ¼ºó¸´ÖÆ

¸Ã´úÂë½ç˵ÁËUserÄ£×Ó¹¤³§µÄĬÈÏÊôÐÔÖµ¡£ÎÒÃÇ¿ÉÒÔƾ֤ÐèÒªÐ޸ĻòÌí¼ÓÊôÐÔÖµ¡£

2¡¢Ê¹ÓÃÄ£×Ó¹¤³§µÄ¹ØÁª¹Øϵ

ÈôÊÇÎÒÃÇÐèÒª²âÊÔÄ£×ÓÖ®¼äµÄ¹ØÁª¹Øϵ £¬Ä£×Ó¹¤³§Ò²¿ÉÒÔÌṩÀû±ãµÄÖ§³Ö¡£¼ÙÉèÎÒÃÇÐèÒª²âÊÔÓû§ºÍÎÄÕÂÖ®¼äµÄÒ»¶Ô¶à¹Øϵ £¬¿ÉÒÔʹÓÃÒÔÏ´úÂ뽨ÉèÓû§ºÍÎÄÕÂÄ£×ÓʵÀý£º

$user = factory(User::class)->create();
$post = factory(Post::class)->create(['user_id' => $user->id]);

µÇ¼ºó¸´ÖÆ

´Ë´úÂ뽫½¨ÉèÒ»ÌõÊôÓÚ¸ÃÓû§µÄÎÄÕ £¬²¢½«ÎÄÕÂÏà¹ØµÄuser_idÉèÖÃΪ¸ÃÓû§µÄidÖµ¡£

ÈôÊÇÎÒÃÇÐèÒª²âÊÔÓû§ºÍÎÄÕÂÖ®¼äµÄ¶à¶Ô¶à¹Øϵ £¬¿ÉÒÔʹÓÃÒÔÏ´úÂ뽨ÉèÓû§ºÍ½ÇÉ«Ä£×ÓʵÀý£º

$user = factory(User::class)->create();
$role = factory(Role::class)->create();
$user->roles()->attach($role);

µÇ¼ºó¸´ÖÆ

¸Ã´úÂ뽨ÉèÁËÒ»¸öÓû§ÊµÀýºÍÒ»¸ö½ÇɫʵÀý £¬²¢½«Á½¸öÄ£×Ó¹ØÁªÆðÀ´¡£

×ܽ᣺

ÔÚʹÓÃLaravel¾ÙÐÐWeb¿ª·¢Ê± £¬²âÊÔÊDz»¿ÉºöÂÔµÄÒ»²¿·Ö¡£Ä£×Ó¹¤³§×÷ΪLaravel²âÊÔ¹¤¾ßÖеÄÖ÷Òª×é³É²¿·Ö £¬¿ÉÒÔ¼ò»¯²âÊÔÀú³Ì £¬ïÔÌ­´úÂëÈßÓà £¬²¢Ìá¸ß²âÊԵĿɿ¿ÐÔ¡£Ê¹ÓÃÄ£×Ó¹¤³§²âÊÔÊý¾Ý¿âÊÇLaravel¿ª·¢Öв»¿É»òȱµÄÒ»»· £¬ÖµµÃÎÒÃÇÔÚÏÖʵ¿ª·¢Öгä·ÖÑéÕ¹ÆäÓÅÊÆ¡£

ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃÄ£×Ó¹¤³§²âÊÔÊý¾Ý¿â £¿µÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

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