Create Custom Factory (ok)
https://laravel.com/docs/9.x/seeding
Tổng hợp nguồn để lấy trường facker https://app.gitbook.com/o/-LVfVRgJVnpA6bttpEFT/s/-MCBeDUD-PK_RF-YgJRe/tong-hop-nhung-truong-du-lieu-de-facker-ok
Cách 1:
php artisan make:seeder UserSeeder
C:\xampp\htdocs\api\database\seeders\UserSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class UserSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run() {
DB::table('users')->insert([
'name' => Str::random(10),
'email' => Str::random(10) . '@gmail.com',
'password' => Hash::make('password'),
]);
}
}
Run
php artisan db:seed --class=UserSeeder
Kết quả:

Chú ý: Theo một ví dụ mặc định
C:\xampp\htdocs\api\database\seeders\DatabaseSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder {
/**
* Seed the application's database.
*
* @return void
*/
public function run() {
\App\Models\User::factory(10)->create();
}
}
Chỉ cần chay dòng lệnh
php artisan db:seed
Cách 2: đ
php artisan make:migration item --create=Item
C:\xampp\htdocs\api\database\migrations\2022_04_24_190230_item.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Item extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('Item', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->string('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists('Item');
}
}
php artisan make:model Item
C:\xampp\htdocs\api\app\Models\Item.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Item extends Model {
use HasFactory;
public $timestamps = false;
protected $table = 'item';
protected $primaryKey = 'id';
protected $fillable = ['title', 'body'];
}
php artisan make:seeder ItemSeeder
C:\xampp\htdocs\api\database\seeders\ItemSeeder.php
<?php
namespace Database\Seeders;
use App\Models\Item;
use Illuminate\Database\Seeder;
class ItemSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run() {
Item::factory()->count(5)->create();
}
}
php artisan make:factory ItemFactory --model=Item
C:\xampp\htdocs\api\database\factories\ItemFactory.php
<?php
namespace Database\Factories;
use App\Models\Item;
use Illuminate\Database\Eloquent\Factories\Factory;
class ItemFactory extends Factory {
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Item::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
return [
'title' => $this->faker->title,
'body' => $this->faker->text,
];
}
}
Sau cùng chạy:
php artisan db:seed --class=ItemSeeder
Kết quả thật vi diệu :)
Previous==== END LARAVEL 9 ====NextGiới hạn số bài viết $category_posts = Post::where('post_category_id',$id)->take(6)->get() (ok)
Last updated
Was this helpful?