Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
bug in laravel 8.4? belongsToMany relationship objects not saved inside transaction

I have a simple transaction structure and a belongsToMany-relationship between 2 models user and sponsoring. I found out that the relationship data are not saved in the pivot table, when I wrap it inside a transaction. Without a transaction it works fine. Is this a zero day bug in laravel (framework v8.40) or does it work somehow different?

My models are defined like this:


class Sponsoring extends Model
{
    use HasFactory;
    use SoftDeletes;

    public function users()
    {
        return $this->belongsToMany(User::class, 'user_sponsorings');
    }

  // ...
}

class User extends Authenticatable implements HasLocalePreference
{
    use SoftDeletes;
    use HasFactory;
    use Notifiable;

    public function sponsorings()
    {
        return $this->belongsToMany(Sponsoring::class, 'user_sponsorings');
    }

  // ...
}

My code looks like:

//DB::beginTransaction();

try {
$sponsoring = new Sponsoring();
// ...
$sponsoring->save();

$userIds = [ Auth::user()->id ];
// ...
$sponsoring->users()->sync($userIds);

} catch (\Exception $ex) {
   //DB::rollBack();
   throw new Error('could not save sponsoring: ' . $ex->getMessage());
 }

 //DB::commit();

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!