belongsToMany with updateOrCreate

Posted 2 years ago by eddieace

Hi I have this function. It runs every once in a while and I just added every location that a postition have. In a pivot table. The problem now is that every time it runs it adds a new record in the database. So the position gets new locations every time it runs.

Here is the function:

$position = Position::updateOrCreate(['title' => $position->title, company_id => $company_id], $position);

foreach($position->locations() as $location) {

    $location = Location::firstOrCreate(['name' => $location->name]);

Is there some way I could

if (if position_location not already exists) {

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.