John Davidson

php - I'm getting an error 'Undefined variable:factory' when using the command shown

0 comments
Message:


I'm new to Laravel and I've been trying to generate fake data into my DB using Laravel but I'm getting the error Undefined variable:factory. Can the following command still be used in laravel8? If not, which other code can be used to display data?


CategoryFactory

$factory->define(Category::class,function (Faker $faker){
$title = $faker->sentence;

return[
'title'=>$this->faker->sentence,
'slug'=>Str::slug($title),
];

});

Category Seeders

namespace Database\Seeders;
use App\Models\Category;
use Illuminate\Database\Seeder;

class CategorySeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Category::factory()->count(10)->create();
}
}

Category Migration

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

class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string("title");
$table->string("slug",191)->unique();
$table->timestamps();
});
}

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


My Answer:
PHP is a popular server-side scripting language that is used to create dynamic web pages. It is often used in conjunction with HTML to add functionality to websites, such as processing form data, interacting with databases, and generating dynamic content. PHP stands for Hypertext Preprocessor and is open-source, meaning it is freely available for anyone to use and modify.

Rate this post

5 of 5 based on 5134 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us