2

Hi I am new to creating factories on Laravel and I am setting up a blog website. I had this going just fine and now it isn't working anymore.

When I use "tinker" to add fake data using (App/Models/Post::factory(30)->create();), below error keep occurs. Could anyone please educate me why this error occurs?

App/Models/Post::factory(30)->create(); 
PHP Warning: Use of undefined constant App - assumed 'App' (this will throw an Error in a future version of PHP) in F:\coding\laravel\test-laraveleval()'d code on line 1 
PHP Warning: Use of undefined constant Models - assumed 'Models' (this will throw an Error in a future version of PHP) in F:\coding\laravel\test-laraveleval()'d code on line 1 
PHP Warning: A non-numeric value encountered in F:\coding\laravel\test-laraveleval()'d code on line 1 
PHP Warning: A non-numeric value encountered in F:\coding\laravel\test-laraveleval()'d code on line 1 
PHP Warning: Division by zero in F:\coding\laravel\test-laraveleval()'d code on line 1 
[!] Aliasing 'Post' to 'App\Models\Post' for this Tinker session. 
Illuminate\Database\QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'sint' for key 'categories_name_unique' (SQL: insert into categories (name, slug, updated_at, created_at) values (sint, ut-quisquam-et-et-tenetur-molestias-in-dolor-voluptatum, 2022-03-16 07:11:05, 2022-03-16 07:11:05))

'

Malith2007
  • 21
  • 1
  • In `php artisan tinker`, try either `Post::factory(30)->create()`, or `\App\Models\Post::factory(30)->create()`. If neither of those works, make sure you have a `Post.php` file within `app/Models`, with the namespace `namespace App\Models;`. Also, in the future, please include code for the Model in your question; it'll help us debug this faster. – Tim Lewis Mar 16 '22 at 17:40
  • 1
    You're using forward slash ( / ) when you should be using back slash ( \ ). `App\Models\Post::factory(30)->create()` – IGP Mar 16 '22 at 19:27
  • This is really helpful. Thanks. Next time I'll add the relevant codes also. – Malith2007 Mar 17 '22 at 04:55

1 Answers1

0

You have to define factory rules such that you have a constraint in your database that checks for category name uniqueness.

You should read how to write a custom factory in Laravel 8:Examples how to generate factories. enter image description here