2

I want to generate dummy data using factory with seeder so it will give me the error. when I run this command given below: php artisan db:seed

so here it's the error.

PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 262144 bytes) in D:\xampp\htdocs\Bootstrap\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 1120 PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 262144 bytes) in Unknown on line 0

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        factory(User::class,5)->create()->each(function ($user){
            $profile = factory(Profile::class)->make();
            $user->profile()->save($profile);
            $profile->each(function ($profiles){
                $qualification =factory(Qualification::class,3)->make();
                $experience =factory(Experience::class,3)->make();
                $profiles->qualification()->saveMany($qualification);
                $profiles->experience()->saveMany($experience);
            });
        });
    }
}

For each User has-one Profile. For each Profile Has-many (Qualification and Experience).

If we run this code given below:

class DatabaseSeeder extends Seeder
{
   
    public function run()
    {
        DB::table('posts')->insertOrIgnore([
            ['id'=>1,'title'=>'admission','created_at'=>now(),'updated_at'=>now()],
            ['id'=>2,'title'=>'biology','created_at'=>now(),'updated_at'=>now()],
            ['id'=>3,'title'=>'mathematics','created_at'=>now(),'updated_at'=>now()],
            ['id'=>4,'title'=>'chemistry','created_at'=>now(),'updated_at'=>now()],
            ['id'=>5,'title'=>'physics','created_at'=>now(),'updated_at'=>now()],
            ['id'=>6,'title'=>'english','created_at'=>now(),'updated_at'=>now()],
            ['id'=>7,'title'=>'urdu','created_at'=>now(),'updated_at'=>now()],
        ]);
        DB::table('provinces')->insertOrIgnore([
            ['id'=>1,'title'=>'punjab','created_at'=>now(),'updated_at'=>now()],
            ['id'=>2,'title'=>'sindh','created_at'=>now(),'updated_at'=>now()],
            ['id'=>3,'title'=>'nwfp','created_at'=>now(),'updated_at'=>now()],
            ['id'=>4,'title'=>'balochistan','created_at'=>now(),'updated_at'=>now()],
        ]);

    }
}

using this command php artisan db:seed then there is no error received.

please help me in using a laravel factory.

Mubashir
  • 75
  • 1
  • 3
  • 9

1 Answers1

-2

To fix that issue edit your php.ini.

Increase the memory limit by 512M >

; Maximum amount of memory a script may consume (128 MB)
; http://php.net/memory-limit

memory_limit=512M

or make it unlimited.(it depends on your server resources)

; Maximum amount of memory a script may consume (128 MB)
; http://php.net/memory-limit

memory_limit=512M
iFart
  • 62
  • 1
  • 4
  • yes I have done this task but there is same Error... – Mubashir Aug 05 '20 at 16:09
  • because I have both tried increasing or decreasing memory limit size of xampp server. – Mubashir Aug 05 '20 at 16:10
  • @Mubashir maybe there's 2 php coexists in your system. 1 in the xampp and the other is in the system. You can check it by `php -ini`. – iFart Aug 07 '20 at 10:57
  • issues found in seeder and (User and Profile model)... so there's mistake in code... when giving this type of Error... – Mubashir Aug 07 '20 at 11:02