skip to Main Content

I am building a lead management system using laravel10 and I had several tables like lead,individualLead,BusinessLead,Statuses,sources,etc The statuses table can have values like Cancelled,Pending,Followup started, Completed etc. While seeding the database the same values are feeded as multiple entries to the table ,how to make sure that the table only has unique and relevant data while seeding the database. I am also building the Api’s for these ta[[[enter image description here](https://phpout.com/wp-content/uploads/2023/10/Wjh5R.png)](https://phpout.com/wp-content/uploads/2023/10/IgA37.png)](https://phpout.com/wp-content/uploads/2023/10/LLUYQ.png)bles too

I had tried to add unique() constrain, as well as looping while seeding the table

2

Answers


    1. Use Faker to Generate Unique Values:

      'contact_no' => $this->faker->unique()->phoneNumber,

      'email' => $this->faker->unique()->safeEmail,

    2.In migration table

        $table->unique(['email']);
        $table->unique(['phoneNumber']);
    
    Login or Signup to reply.
  1. If you are running seeder first time. It will create. In case running again without db:fresh than will update the record.

    you can add all the unique composite keys in the first parameter of updateOrCreate function

     AppUser::updateOrCreate(['user_email','phone'],$data);
    

    Indexing the table column while migration will not allow to create duplicate records.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search