The firstOrCreate method takes two arrays, the first of which is all of the attributes to find an existing record, the second being any additional attributes that a newly created record should get (combined with the values in the first array). So you can specify multiple key/value pairs in both arrays
Jan 9, 2020
3
Level 1
Laravel firstOrCreate with multiple attributes
Hi, when using firstOrCreate method with multiple attributes it keeps using the first attribute to find the record and I don't want that, I need to use multiple attributes to check a record if it doesn't exist in the table, I am using firstOrCreate with multiple attribute for finding the record like this :
$log_pn = Log_panne::firstOrCreate(['n_s' => $request->get('n_serie')], ['panne_id' => $panne_id]);
$log_pn->n_s = $request->get('n_serie');
$log_pn->panne_id = $panne_id;
$log_pn->save();
Is ther a way to do this using Laravel Eloquent ?
Level 104
1 like
Please or to participate in this conversation.