Henry
Henry

Reputation: 1755

Why won't artisan allow me to create data from my model?

I'm rather new to Laravel and am currently running Laravel 8.x on Windows 10. I've created a database table, model, factory and seeder for generating articles for an imaginary blog. (I'm imitating a tutorial on Laravel and Vue that I like on YouTube and also this article).

The article suggests verifying that everything works by going into php artisan tinker and executing the command:

Article::factory()->create();

When I do that, I get this message:

PHP Fatal Error: Class 'Article' not found in Psy Shell code on line 1

I have no idea what that's supposed to mean but I assume it's not happy with my code in some respect. I've looked at it as thoroughly as I can but don't see anything wrong based on the examples in the article. Could someone more knowledgeable in Laravel kindly eyeball this and tell me what I've done wrong? (For what it's worth, I tried User::factory()->create(); and it worked fine.)

Article.php is stored in app/Models and contains:

<?php

namespace App\Models;

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

class Article extends Model
{
    use HasFactory;

    protected $fillable = [
        'title', 'body',
    ];
}

ArticleFactory.php is stored at database/factories and contains:

<?php

namespace Database\Factories;

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

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

    /**
     * Define the model's default state. 
     * 
     * @return array 
     */

    public function definition()  
    {
        return [
            'title' => $this->faker->text(50),
            'body' => $this->faker->text(200)
        ];
    }
}

2020_11_30_034856_create_articles_table is stored at database/migrations and contains:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateArticlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('body');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('articles');
    }
}

Lastly, ArticlesTableSeeder.php is stored at database/seeders and contains:

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class ArticlesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Article::factory()
        ->times(30)
        ->create();
    }
}

Can anyone see why this code isn't working?

EDIT: I tried Patricus' suggestion and it worked fine in tinker. However, when I tried php artisan db:seed, it failed. I got this error:

   Seeding: Database\Seeders\ArticlesTableSeeder

   Error 

  Class 'Database\Seeders\Article' not found

  at C:\Laravel\larticles\database\seeders\ArticlesTableSeeder.php:16
     12▕      * @return void
     13▕      */
     14▕     public function run()
     15▕     {
  ➜  16▕         Article::factory()
     17▕         ->times(30)
     18▕         ->create();
     19▕     }
     20▕ }

  1   C:\Laravel\larticles\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:36
      Database\Seeders\ArticlesTableSeeder::run()

  2   C:\Laravel\larticles\vendor\laravel\framework\src\Illuminate\Container\Util.php:40
      Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()

Upvotes: 0

Views: 533

Answers (1)

patricus
patricus

Reputation: 62228

Tinker doesn't run in a namespace. While it tries to help you out with some things related to models, it can't autocorrect namespaces on static method calls.

Your model is \App\Models\Article. In tinker, you need to call:

\App\Models\Article::factory()->create();

Upvotes: 2

Related Questions