简体   繁体   中英

PHP Error: Class 'Page' not found in laravel 8 tinker

I'm Inserting some dummy data using the factory model class using this command on tinker: When i run

composer dump-autoload,
php artisan tinker,  
Page::factory(10)->create()

Then this error shows

PHP Error: Class 'Page' not found in /var/www/html/laravel/laravel8-blogeval()'d code on line 1

image description here

My model file location app\Models\Page.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory; 
use Illuminate\Database\Eloquent\Model;

class Page extends Model {
    use HasFactory;
     /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title',
        'slug',
        'body',
        'excerpt',
        'image',
        'thumb',
        'view_count',
        'user_id',
        'meta_keywords',
        'meta_description',
        'social_image',
        'order',
        'published_at',
        'is_active',
        'is_destroy'
    ]; }

My factory file location database/factories/PageFactory.php

<?php

namespace Database\Factories;

use App\Models\Page;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class PageFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Page::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        $title = $this->faker->title;
        $slug = Str::slug($title);
        $user = User::count() >= 20 ? User::inRandomOrder()->first()->id: User::factory();   
      
        return [
            'title'=> $title,
            'slug' => $slug,
            'body' => $this->faker->text(300),
            'image' => $this->faker->imageUrl(900, 300),
            'user_id' => $user,
        ];
    }
}

How to insert dummy data in Laravel 8 using tinker please? Thank you.

I recommend using full path for your model, however you can't pass count to factory directly, you should use count method:

App\Models\Page::factory()->count(10)->create();

Run this command:

composer dump-autoload,
php artisan tinker,  
Page::factory(10)->create()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM