0
I’m trying to automatically register a value in the database using Laravel. I’m struggling to think of an efficient solution to this. Below is the example of what I want.
$email = '[email protected]';
MeuModel::create(array_merge($request->all(), ['email'=>$email] ));
My logic is this:: As long as the value of $email
within the database, register the value of the $email
. If the value of $email
is in the database, do not register this value. I want to find the solution using the models from Laravel. You could help me?
The method
firstOrCreate()
only works if the data is all the same, for example if you try to insert['name' => 'Fulano', 'email' => '[email protected]']
but in your database already exists the email "[email protected]" but his name is saved as "Fulano Silva", the methodfirstOrCreate()
will understand how 2 different records and will save duplicate email in your bank.– Kayo Bruno
Emmanuel, your answer was helpful. Try to develop it better for me to give you a positive.
– Arthur Abitante