php artisan make:model Name -mcr
'something' => $faker->randomElement(['foo' ,'bar', 'baz'])
$factory->define(Model::class, function (Faker $faker) {
/**
* Set up the type of organisation.
*/
$type = $faker->randomElement(['organisation', 'charity']);
/**
* Set the charity number to null by default.
*/
$charityNumber = null;
/**
* Check the type of organisation, and if charity, create dummy number.
*/
if($type == 'charity') {
$charityNumber = $faker->numberBetween(100000,999999) . '-0';
}
return [
'name' => $faker->company,
'type' => $type,
'charityNumber' => $charityNumber
];
});
You can ensure that any data randomly generated by faker is unique by using the unique()
method, like so:
return [
'email' => $faker->unique()->email,
]
When using Laravel 5.8 and above, and you want to use the setUp method, you need to return it as : void. E.g.
class ProductTest extends TestCase
{
protected $product;
public function setUp() : void
{
$this->product = new Product();
$this->product->manufacturer = 'Apple';
$this->product->name = 'iPhone 11 Pro';
$this->product->price = '4999';
}
public function testProductHasName()
{
$this->assertEquals('iPhone 11 Pro', $this->product->name());
}
}