Questions tagged [laravel-factory]

66 questions
24
votes
3 answers

Defining Laravel foreign keys with Model Factories, One to One & One to Many Relationships without creating unnecessary models

Recently I have been trying to seed my database using Laravel seeding through Model Factories and Faker. For simple schemas, it is just a breeze to have it working :). However, I have encountered several problems when working with complex DB schemas…
andcl
  • 3,342
  • 7
  • 33
  • 61
7
votes
2 answers

Laravel 8 - Factories & Foreign Keys

I haven't been able to locate how to manage foreign keys when creating Factories and Seeders. I have a users table and a blog_posts table. The blog_posts table has a foreign key user_id that references users.id. I would like to seed my database with…
Newb 4 You BB
  • 1,205
  • 1
  • 11
  • 30
5
votes
3 answers

Factory creating multiple models with different number of relationships

I'm using the following code to create 20 posts, each of which has 3 comments. Post::factory() ->times(20) ->has(Comment::factory()->times(3)) ->create() Instead I'd like to create 20 posts, each of which has a random number of comments…
jose
  • 1,490
  • 3
  • 30
  • 65
5
votes
2 answers

Laravel 8 factory class is not overriding the parameters while creating the factories

I am developing a web application using Laravel 8. I have noticed that quite a lot of things have changed in Laravel 8 including factories. I have a factory class MenuCategoryFactory for my MenuCategory model class with the following…
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
5
votes
2 answers

How to get unique values from faker?

I would like to ask how to generate unique value from faker? I know this is a familiar question actually, you may put some duplicate links e.g. link 1, link 2 but unfortunately, these links does not answer my problem. Here is my code below. I tried…
schutte
  • 1,949
  • 7
  • 25
  • 45
5
votes
6 answers

Laravel 8 Multiple Relationships for Factory

In Laravel 8 it is possible to quickly fill relationships with factories. However, I cannot figure out how to generate more than one relationship. How can I create a random or new relationship for each link using the new Laravel 8 syntax? This…
wizardzeb
  • 1,546
  • 2
  • 14
  • 30
4
votes
1 answer

Laravel factory Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given

I get this error when trying to run a factory in laravel 8. I've looked though several posts about this error but they all seem to come from saving/creating directly incorrectly. Not using a factory. So I am not sure why the factory isn't saving it…
user16092281
3
votes
1 answer

How with laravel factory add data to related table?

In laravel 8 app I add dummy data for ads table with factory: $ads = Ad::factory()->count(10)->expired($year, $month)->create([ ]); And with database/factories/AdFactory.php:
mstdmstd
  • 2,195
  • 17
  • 63
  • 140
3
votes
1 answer

I got error array_merge Expected parameter 2 to be in laravel 8 factory?

In Laravel 8.12 running factory from controller I got error : [2021-01-12 16:42:10] local.ERROR: array_merge(): Expected parameter 2 to be an array, int given {"exception":"[object] (ErrorException(code: 0): array_merge(): Expected parameter 2 to be…
mstdmstd
  • 2,195
  • 17
  • 63
  • 140
3
votes
1 answer

How to pass parameters to laravel factory in database seeder?

Is it possible to pass data from seeder to factory? This is my PictureFactory: class PictureFactory extends Factory{ protected $model = Picture::class; public function definition($galleryId = null, $news = false){ if…
3
votes
1 answer

Call to undefined method newCollection when using factories with Laravel-Doctrine

I would like to use Model factories, but I get this error: 1) Tests\Factory\UserFactoryTest::testUserCount Error: Call to undefined method…
Iter Ator
  • 8,226
  • 20
  • 73
  • 164
2
votes
4 answers

How to send a value as parameter to the Factory Class

I need to run a Factory 50 times, so inside the DatabseSeeder: public function run() { for($i=1;$i<=50;$i++){ (new CategoryQuestionFactory($i))->create(); } } So as you can see, I tried passing a variable called $i as parameter to…
Pouya
  • 114
  • 1
  • 8
  • 36
2
votes
1 answer

Laravel factory: recycle method not randomize input collection

It says in the documentation that "The recycle method also accepts a collection of existing models. When a collection is provided to the recycle method, a random model from the collection will be chosen when the factory needs a model of that…
Fendy Harianto
  • 267
  • 1
  • 8
2
votes
1 answer

Laravel 7 model factory is throwing "InvalidArgumentException: Unknown formatter" exception for faker

I started building a web application using Laravel 8. I have noticed that a couple of things have changed in the Laravel 8 including model factory. Now, I am writing a unit test using factories for models. But it is throwing error when I fake the…
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
2
votes
2 answers

Laravel 7: Create a custom factory for category

I was trying to make the following category factory to work. Category factory
itwolfpower
  • 306
  • 3
  • 11
1
2 3 4 5