Ƙirƙirar Data Amfani Seeder a Laravel

A cikin Laravel, seeder ana amfani da su don cika ma'ajin bayanai tare da bayanan farko ko na ɓarna. Suna samar da hanya mai dacewa don ƙirƙira da saka bayanai a cikin tebur ɗin bayanai. Anan ga jagorar mataki-mataki akan amfani seeder a cikin Laravel:

 

Ƙirƙiri a Seeder

Don ƙirƙirar sabon seeder, zaku iya amfani da umarnin Artisan. Misali, don ƙirƙirar tebur don “masu amfani”, gudanar da umarni mai zuwa: make:seeder seeder

php artisan make:seeder UsersTableSeeder

 

Ƙayyadaddun Bayanai

Bude seeder fayil ɗin da aka ƙirƙira a cikin  kundin adireshi. A cikin hanyar, zaku iya ayyana bayanan da kuke son shukawa a cikin rumbun adana bayanai. Kuna iya amfani da maginin tambaya ko Eloquent ORM don saka bayanan. database/seeders run Laravel

public function run()  
{  
    DB::table('users')->insert([  
        [  
            'name' => 'John Doe',  
            'email' => '[email protected]',  
            'password' => bcrypt('password123'),  
        ],  
        [  
            'name' => 'Jane Doe',  
            'email' => '[email protected]',  
            'password' => bcrypt('password456'),  
        ],  
        // Add more data as needed  
    ]);  
}  

 

Gudu da Seeder

Don aiwatar da seeder da saka bayanai a cikin bayanan, yi amfani da db:seed umarnin Artisan. Ta hanyar tsoho, duk seeder za a gudanar. Idan kuna son gudanar da takamaiman seeder, zaku iya amfani da --class zaɓin.

php artisan db:seed

 

Seeder kuma Rollback

Seeder ana iya juya baya kamar ƙaura. Don soke rukunin ƙarshe na seeder, zaku iya amfani da db:seed --class umarni tare da --reverse zaɓi.

 

Yin amfani seeder da ciki Laravel yana sauƙaƙa don cika rumbun adana bayanai tare da bayanan farko ko ƙirƙirar bayanan ɓoyayyiya don dalilai na gwaji. Yana ba ku damar shigar da bayanai da sauri cikin tebur ba tare da sa hannun hannu ba.