Faker Images, numberBetween(1, 20) in Laravel, migration (ok)
https://5balloons.info/faker-images-in-laravel/
Một số ví dụ đã thực hành 👍👍👍) migration
php artisan make:factory Product --model=Product
php artisan make:seeder PermissionTableSeeder
php artisan db:seed --class=ProductSeeder
Laravel Factory: Manual Increment of Column, Number
class AliasCommandFactory extends Factory {
private static $order = 1;
protected $model = AliasCommand::class;
public function definition() {
$faker = $this->faker;
return [
'user_id' => User::inRandomOrder()->first()->id,
'command' => $faker->word,
'content' => $faker->sentence,
'order' => self::$order++
];
}
}
Models
C:\Users\Administrator\Downloads\test\app\Models\User.php
<?php
namespace App\Models;
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',
];
/**
* Get the phone associated with the user.
*
* Syntax: return $this->hasOne(Phone::class, 'foreign_key', 'local_key');
*
* Example: return $this->hasOne(Phone::class, 'user_id', 'id');
*/
public function phone() {
return $this->hasOne(Phone::class,'user_id', 'id');
}
}
C:\Users\Administrator\Downloads\test\app\Models\Post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
use HasFactory;
public function comments() {
return $this->hasMany(Comment::class);
}
}
C:\Users\Administrator\Downloads\test\app\Models\Phone.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Phone extends Model {
use HasFactory;
/**
* Get the user that owns the phone.
*
* Syntax: return $this->belongsTo(User::class, 'foreign_key', 'owner_key');
*
* Example: return $this->belongsTo(User::class, 'user_id', 'id');
*/
// public $timestamps = false;
// protected $table = 'phones';
// protected $primaryKey = 'id';
protected $fillable = [
'user_id', 'phone'
];
public function user() {
return $this->belongsTo(User::class,'user_id', 'id');
}
}
C:\Users\Administrator\Downloads\test\app\Models\Comment.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model {
use HasFactory;
public function post() {
return $this->belongsTo(Comment::class,'post_id','id');
}
}
Factories
C:\Users\Administrator\Downloads\test\database\factories\CommentFactory.php
<?php
namespace Database\Factories;
use App\Models\Comment;
use Illuminate\Database\Eloquent\Factories\Factory;
class CommentFactory extends Factory {
protected $model = Comment::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
return [
'post_id' => $this->faker->numberBetween(1,500),
'comment' => $this->faker->text(100),
];
}
}
C:\Users\Administrator\Downloads\test\database\factories\PhoneFactory.php
<?php
namespace Database\Factories;
use App\Models\Phone;
use Illuminate\Database\Eloquent\Factories\Factory;
class PhoneFactory extends Factory {
protected $model = Phone::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
return [
'user_id' => $this->faker->numberBetween(1, 30),
'phone' => $this->faker->phoneNumber,
];
}
}
C:\Users\Administrator\Downloads\test\database\factories\PostFactory.php
<?php
namespace Database\Factories;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
class PostFactory extends Factory {
protected $model = Post::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
return [
'name' => $this->faker->text(10),
];
}
}
C:\Users\Administrator\Downloads\test\database\factories\UserFactory.php
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class UserFactory extends Factory {
/**
* Define the model's default state.
*
* @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),
];
}
/**
* Indicate that the model's email address should be unverified.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function unverified() {
return $this->state(function (array $attributes) {
return [
'email_verified_at' => null,
];
});
}
}
Migrations
C:\Users\Administrator\Downloads\test\database\migrations\2014_10_12_000000_create_users_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists('users');
}
}
C:\Users\Administrator\Downloads\test\database\migrations\2022_05_05_171115_create_phones_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePhonesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('phones', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained('users');
$table->string('phone');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists('phones');
}
}
C:\Users\Administrator\Downloads\test\database\migrations\2022_05_05_180005_create_posts_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string("name");
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists('posts');
}
}
C:\Users\Administrator\Downloads\test\database\migrations\2022_05_05_180155_create_comments_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCommentsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->foreignId('post_id')->constrained('posts');
$table->string("comment");
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists('comments');
}
}
Seeders
C:\Users\Administrator\Downloads\test\database\seeders\CommentSeeder.php
<?php
namespace Database\Seeders;
use App\Models\Comment;
use Illuminate\Database\Seeder;
class CommentSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run() {
Comment::factory()->count(5000)->create();
}
}
C:\Users\Administrator\Downloads\test\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();
}
}
C:\Users\Administrator\Downloads\test\database\seeders\PhoneSeeder.php
<?php
namespace Database\Seeders;
use App\Models\Phone;
use Illuminate\Database\Seeder;
class PhoneSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run() {
Phone::factory()->count(30)->create();
}
}
C:\Users\Administrator\Downloads\test\database\seeders\PostSeeder.php
<?php
namespace Database\Seeders;
use App\Models\Post;
use Illuminate\Database\Seeder;
class PostSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run() {
Post::factory()->count(500)->create();
}
}
Cai đặt thư viện tạo ảnh 👍👍👍👍)
composer require fzaninotto/faker
Một ví dụ đã hoàn thành về số và ảnh 👍👍 ok
Kết quả: ok
php artisan storage:link

C:\xampp\htdocs\api\database\migrations\2022_04_29_032441_create_posts_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title', 200);
$table->string('short_desc', 250);
$table->string('desc', 255);
$table->string('image', 200);
$table->string('post_category_id', 200);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists('posts');
}
}
C:\xampp\htdocs\api\database\factories\PostFactory.php
<?php
namespace Database\Factories;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
class PostFactory extends Factory {
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Post::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
return [
'title' => $this->faker->text,
'short_desc' => $this->faker->text,
'desc' => $this->faker->address,
'image' => $this->faker->image('public/images', 640, 480, null, false),
'post_category_id' => $this->faker->numberBetween(1, 20),
];
}
}
C:\xampp\htdocs\api\database\seeders\PostSeeder.php
<?php
namespace Database\Seeders;
use App\Models\Post;
use Illuminate\Database\Seeder;
class PostSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run() {
Post::factory()->count(5)->create();
}
}
C:\xampp\htdocs\api\database\factories\PostFactory.php
<?php
namespace Database\Factories;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
class PostFactory extends Factory {
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Post::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
return [
'title' => $this->faker->text,
'short_desc' => $this->faker->text,
'desc' => $this->faker->address,
'image' => $this->faker->image('public/images', 640, 480, null, false),
'post_category_id' => $this->faker->numberBetween(1, 20),
];
}
}
Run:
php artisan db:seed --class=PostSeeder
php artisan storage:link
<?php
/* @var $factory \Illuminate\Database\Eloquent\Factory */
use App\Product;
use Faker\Generator as Faker;
$factory->define(Product::class, function (Faker $faker) {
return [
'name' => $faker->word,
'short_description' => $faker->sentence,
'description' => $faker->paragraph,
'category_id' => function () {
return factory(App\Category::class)->create()->id;
},
'amount' => $faker->randomFloat(2, 0, 10000),
'image' => $faker->image('public/storage/images',640,480, null, false),
];
});
Now when you run your factory method from tinker or Seed class, it will generate a random image for you and will store it in public/storage/images
directory.
This is how you can reference the images on the front-end
<img src="/storage/images/{{$product->image}}">
Last updated
Was this helpful?