Merhaba arkadaslar.
Laravel'de cok yeniyim ))
Uzun calismalardan sonra bilgisayara PHP 8.2 surumunu kurarak Laravel 10 ile calismaya basladim.
Users ve Products adli 2 tablo icerisine Seeding ile faker kullanarak mesela 100 veri eklemek istiyorum. Bir turlu calismiyor. Oncesinde Laravel 8 ile calisdigim icin kodlari yanlis yaziyorum deye dusunuyorum. Ornek olarak, Products adli Model, Factory ve Seeder dosyalarini ekliyorum.
Products Table:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name', 100);
$table->string('slug', 100);
$table->text('description')->nullable();
$table->double('price');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('products');
}
};
Product Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
//protected $table = 'products';
//protected $fillable = ['name', 'slug', 'price'];
protected $guarded = [];
//protected $fillable = ['name', 'slug', 'description', 'price'];
}
Product Factory:
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Product>
*/
class ProductFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => fake()->name(),
'slug' => fake()->unique()->slug(1),
'description' => fake()->paragraph(2),
'price' => Str::random(3)
];
}
}
Product Table Seeder:
<?php
namespace Database\Seeders;
use App\Product;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class ProductTableSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
DB::statement("TRUNCATE TABLE users");
factory(Product::class, 10)->create();
}
}
Simdi Seeding ile gelen hata: KOD: App\Product::factory()->count(100)->create();
> App\Product::factory()->count(100)->create();
>
> Error Call to undefined method App\Product::factory().
php artisan db:seed ile gelen hata:
Database\Seeders\UserTableSeeder ......................................................................................................... RUNNING
> InvalidArgumentException
>
> Unable to locate factory for [App\Models\User].
>
> at vendor\laravel\legacy-factories\src\FactoryBuilder.php:273
> 269▕ */
> 270▕ protected function getRawAttributes(array $attributes = [])
> 271▕ {
> 272▕ if (! isset($this->definitions[$this->class])) {
> ➜ 273▕ throw new InvalidArgumentException("Unable to locate factory for [{$this->class}].");
> 274▕ }
> 275▕
> 276▕ $definition = call_user_func(
> 277▕ $this->definitions[$this->class],
>
> 1 vendor\laravel\legacy-factories\src\FactoryBuilder.php:296
> Illuminate\Database\Eloquent\FactoryBuilder::getRawAttributes([])
>
> 2 vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\GuardsAttributes.php:149
> Illuminate\Database\Eloquent\FactoryBuilder::Illuminate\Database\Eloquent\{closure}()
Deyerli arkadaslar, lutfen yardim edin. Yeni baslamisim zaten