Laravel Faker: Seed Unique Values in Factories

preview_player
Показать описание
I want to show a few examples of how Faker's library unique() actually works: with a single field, or with a pivot table for unique multiple columns.

- - - - -
Support the channel by checking out our products:
Рекомендации по теме
Комментарии
Автор

Great video. I may use some of this to re-write a seeder where I was doing a "Not exists" check.

JeffRose
Автор

Happy holiday everyone, success to everyone in everything

L-A-O-S
Автор

Thank you! The solution that I've been finding.

One more question

How to make autocomplete the timestamps when u do migrate?

I mean now if u try to add timestamps column to migration

$table->timestamps();

and migrate it u will get null in DataBase.

sardorjs
Автор

*this should help stopping those tests that work 99% of the time but sometimes fail*

CODINGISEASY
Автор

Hi Povilas

Just wondering if you have any new year sales planned on your courses?

I'm finding myself completely lost with Laravel and think your courses would be just the medicine the doctor ordered.

ideaphorist
Автор

Hello sir, I've this error "Class not found" when I use
I've been watching your tutorials since few time but it's very helpful.
Thank you.

ibrahimakhaliloulayediallo
Автор

Hello, I am trying to use factory to create 2 tables which must have unique fields but when using unique(true) it gives me the error of duplicate fields. Any advice ?

miguelangelmendoza
Автор

when to use factory or seeder sir, In my case I normally used seeders

geneartista
Автор

I'm trying to use Faker to generate random images but always return false. This is my code $this->faker->image('public/images/avatars', 200, 200, null, false) what wrong with that, Sir?

oxadon
Автор




$table->unique(['product_id', 'filter_group_id', 'filter_id']);

Model:
public function product_to_filters(){
return $this->belongsToMany(Filter::class, 'product_to_filters', 'product_id',
}

Seeder:
$filters = Filter::pluck('filter_group_id', 'id'); // [ 'id' => 'filter_group_id' ] - filter_group_id повторяются поэтому он стоит в роле value, а не key


$faker->unique()->randomElements($filters, 4),
);

SQL:
insert into `product_to_filters` (`filter_id`, `product_id`) values (3, 1), (6, 1), (3, 1), (3, 1)

General error:
1364 Field 'filter_group_id' doesn't have a default value")

how to make it work properly? What do u advice me to do in this case? I need more than one parameter to insert. Please help

sardorjs