K
K
Konstantin Timosheno2019-04-20 09:27:10
Laravel
Konstantin Timosheno, 2019-04-20 09:27:10

detach() not working correctly in laravel 5.7?

Good day. Which day I fight over a bug.
In general, there are models of users and payments. A many-to-many relationship is used. The payment is attached to the user in the pivot table.

Communication with the user model

/*
     * Связь с таблицей платежей пользователей
     *
     */
    public function pays(){
      return $this->belongsToMany(Pay::class,
            'pay_users',
            'user_id',
            'pays_id'
        )->withPivot('id', 'date', 'summ', 'doc');
    }

Communication with the payment model

public function users(){
        $this->belongsToMany(User::class,
            'pay_users',
            'pays_id',
            'user_id')->withPivot('id', 'date', 'summ', 'doc');
    }

Removal

public function destroy($id, $value)
    {

        $user = User::find($id);

        $user->pays()->detach($value);

        return redirect()->route('admin.user-pays.index', $id);
    }

Attached everything is OK, but not always removed. Deletes normally when you roll back the database
php artisan migrate:reset
. After this rollback - deletes, but not all, selectively. I checked the id that come, everything is correct. There are no errors in the console or logs. What could be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Timosheno, 2019-04-22
@kastembay

Made it rough and rude

DB::table('pay_users')
            ->where('id', $payId)
            ->take(1)
            ->delete();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question