<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## laravel 的模型工廠使用 場景 laravel 8之前的都是使用的文件類型,后面laravel 8之后改為類的形式了 文檔:https://learnku.com/docs/laravel/8.x/database-testing/9416 ___ **為了簡化升級過程,我們發布了一個[laravel/legacy-factories](https://github.com/laravel/legacy-factories)擴展包,可以在 Laravel 8 中支持以前的模型工廠**。 安裝指令 :**`composer require laravel/legacy-factories`** ___ 安裝后可以使用以前的模型工廠,如果想使用新的工廠按照對應模型的目錄生成對應目錄的模型工廠 如果提示找不到模型工廠那么可以嘗試一下重新加載 執行名稱 **`composer dump-autoload`** ___ namespace Database\Factories; use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Str; class UserFactory extends Factory { /** * 工廠對應模型名稱 * * @var string */ protected $model = User::class; /** * 定義模型默認狀態 * * @return array */ public function definition() { return [ 'name' => $this->faker->name, 'email' => $this->faker->unique()->safeEmail, 'email_verified_at' => now(), 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password 'remember_token' => Str::random(10), 'staus' => 1 ]; } /** * 標識用戶「 已暫停 」狀態。 */ public function forbid() { return $this->state([ 'status' => 0 ]); } } 對應模型 其中的 `Illuminate\Database\Eloquent\Factories\HasFactory` 的用于模型工廠的使用 如果需要使用就要加上這個trait.其中值得注意這個trait中有一個方法是提供給用戶使用指定工廠的 `protected static function newFactory()` ___ ~~~ <?php namespace App\Models; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable; /** * The attributes that are mass assignable. * * @var array<int, string> */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for serialization. * * @var array<int, string> */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast. * * @var array<string, string> */ protected $casts = [ 'email_verified_at' => 'datetime', ]; /** * Create a new factory instance for the model. * * @return \Illuminate\Database\Eloquent\Factories\Factory */ protected static function newFactory() { // return UserFactory::new(); } } ~~~ ___ HasFactory 特征的 factory 方法將使用默認的約定來確定模型的正確工廠。 具體來說,該方法將在 Database \ Factories 命名空間中尋找一個工廠,該工廠的類名與模型名稱匹配,并且后綴為 Factory 。 如果這些約定不適用于您的特定應用程序或工廠,則可以直接使用工廠來創建模型實例。 要使用 factory 類創建一個新的工廠實例,應在工廠上調用靜態的 new 方法: ~~~ use Database\Factories\UserFactory; $users = UserFactory::new()->count(3)->make(); ~~~ 生成一個用戶模型工廠 ~~~ use App\Models\User; User::factory()->carate(); // 數據會添加到數據庫中 User::factory()->make(); // 不會真的生成數據 namespace Tests\Feature; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; use Tests\CreatesApplication; use Illuminate\Foundation\Testing\DatabaseTransactions; abstract class TestCase extends BaseTestCase { use CreatesApplication; use DatabaseTransactions;// 不污染數據庫數據 // use RefreshDatabase; } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看