Samfurin Factory shine mahimmin ƙirar ƙira na software Laravel wanda ke ba ku damar ƙirƙirar abubuwa cikin sassauƙa da sauƙi ba tare da fallasa cikakken ƙirƙirar abu ba logic.
Manufar Factory Tsarin
Tsarin Factory yana ba ku damar ƙirƙirar abubuwa ba tare da sanya su kai tsaye ta amfani da new
kalmar ba. Madadin haka, kuna amfani da factory hanya don ƙirƙirar muku abubuwa.
Factory Tsarin ciki Laravel
A Laravel, Factory ana yawan amfani da Tsarin don samar da bayanan samfur ko bazuwar bayanai don gwadawa ko yawan bayanai. Laravel yana ba da tsarin ginannen Factory tsarin da ke sauƙaƙe ƙirƙirar abubuwa.
Amfani da Factory Pattern in Laravel
Ƙirƙiri Factory: Da farko, kuna buƙatar ƙirƙirar ta Factory amfani da artisan umarnin:
php artisan make:factory ProductFactory
Ƙayyade Factory Logic: A cikin Factory, ayyana logic don ƙirƙirar abubuwa da samar da bayanan samfur don filayen:
use App\Models\Product;
$factory->define(Product::class, function(Faker $faker) {
return [
'name' => $faker->name,
'price' => $faker->randomFloat(2, 10, 100),
// ...
];
});
Amfani da Factory: Kuna iya amfani da abubuwan Factory don ƙirƙirar abubuwa a cikin yanayin da suka dace:
$product = Product::factory()->create();
Fa'idodin Factory Tsarin In Laravel
Ƙirƙirar Abun Abu Logic: Tsarin Factory yana taimakawa keɓance ƙirƙirar abu logic daga babban lambar tushe, yana sa ya zama mai sauƙin kiyayewa.
Ƙirƙirar Bayanan Sauƙi: Kuna iya ƙirƙirar bayanan samfuri don gwaji ko dalilai na haɓaka ta amfani da Factory.
Haɗuwa da Seeder: Factory Yawancin lokaci ana haɗa tsari tare da Seeders don samar da samfurin bayanai yayin shuka bayanai.
Kammalawa
Tsarin Factory a cikin Laravel yana ba ku damar ƙirƙirar abubuwa cikin sassauƙa da sauƙi, samar da bayanan samfur don gwaji ko haɓakawa. Wannan yana haɓaka haɓakawa kuma yana raba ƙirƙira abu logic daga babban codebase.