Reputation: 184
My doubts about the factories and the seeds.
I try to do the migrations with the seeds and I get the following issue. Call to undefined method Database\Factories\PostFactory::faker()
I not have very clear where to make the call, if in a separate file, such as PostSeeds or how I am doing it here.
This is the main file DatabaseSeeder.php
<?php
namespace Database\Seeders;
use App\Models\Category;
use App\Models\Post;
use App\Models\User;
use Illuminate\Support\Facades\Storage;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
// \App\Models\User::factory(10)->create();
Storage::makeDirectory('posts');
User::create([
'name' => 'Fernando',
'email' => '[email protected]',
'password' => '$2y$10$.CjCfp.mzo.AUMPZj0n7mOK3zU6QmrabKXwaYcHNFtK1qWHRqf4Xe',
]);
Category::create([
'name' => 'Plagas',
'slug' => 'plagas',
]);
Category::create([
'name' => 'Enfermedades plantas',
'slug' => 'enfermedades-plantas',
]);
Category::create([
'name' => 'Cultivo ecológico',
'slug' => 'cultivo-ecologico',
]);
Category::create([
'name' => 'Consciencia ecológica',
'slug' => 'consciencia-ecologica',
]);
Post::factory(10)->create();
}
}
this is the main file My Postfactory.php where I do the fakers data.
<?php
namespace Database\Factories;
use App\Models\Category;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
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()
{
$name = $this->faker->unique()->sentence();
return [
'name' => $name,
'slug' => Str::slug($name),
'extract' => $this->faker->text(250),
'body' => $this->faker->text(2000),
'status' => 2,
'category_id' => Category::all()->random()->id,
'user_id' => 1,
'url_image' => 'posts'.$this->faker('public/storage/posts', 650, 490, null, false)
];
}
}
I don't know if it will also be useful to show the migration with the table structure
<?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->string('slug');
$table->text('extract');
$table->longtext('body');
$table->enum('status', [1, 2])->default(1); // 1. Borrador. 2. Publicar.
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('category_id');
$table->string('url_image')->default('default.png');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
Upvotes: 0
Views: 2928
Reputation: 223
Please verify that you have use HasFactory
trait in the Post model and as you are using laravel 8 so, replace your url_image
in Postfactory.php with the following:
'url_image' => $this->faker->image('public/storage/posts', 650, 490, null, false)
Upvotes: 1