PHP Error: Class 'Page' not found in laravel 8 tinker
PHP Error: Class 'Page' not found in laravel 8 tinker
我正在使用工厂模型插入一些虚拟数据 class 在 tinker 上使用此命令:
当我运行
composer dump-autoload,
php artisan tinker,
Page::factory(10)->create()
然后出现这个错误
PHP 错误:Class 'Page' 未在第 1 行的 /var/www/html/laravel/laravel8-blogeval()'d
代码中找到
image description here
我的模型文件位置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'
]; }
我的出厂文件位置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,
];
}
}
如何使用 tinker 在 Laravel 8 中插入虚拟数据?谢谢。
我建议为您的模型使用完整路径,但是您不能直接将计数传递给工厂,您应该使用 count 方法:
App\Models\Page::factory()->count(10)->create();
运行 这个命令:
composer dump-autoload,
php artisan tinker,
Page::factory(10)->create()
我正在使用工厂模型插入一些虚拟数据 class 在 tinker 上使用此命令: 当我运行
composer dump-autoload,
php artisan tinker,
Page::factory(10)->create()
然后出现这个错误
PHP 错误:Class 'Page' 未在第 1 行的 /var/www/html/laravel/laravel8-blogeval()'d
代码中找到
image description here
我的模型文件位置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'
]; }
我的出厂文件位置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,
];
}
}
如何使用 tinker 在 Laravel 8 中插入虚拟数据?谢谢。
我建议为您的模型使用完整路径,但是您不能直接将计数传递给工厂,您应该使用 count 方法:
App\Models\Page::factory()->count(10)->create();
运行 这个命令:
composer dump-autoload,
php artisan tinker,
Page::factory(10)->create()