Saturday, March 11, 2017

laravel eloquent : How to update data if exist

I have a table where i have user_id and role_id in it. Now if I select user_id =1 from dropdown and role_id = 2 from dropdown and save it ..Next time if I want to set same user another role instead of creating new row in table..How to update it without creating new one? can anyone suggest something?

$roleUser = RoleUser::firstOrNew(
            ['role_id' => $request->Input(['role_id']),
             'user_id' =>$request->Input(['user_id'])] );
            $roleUser->save();

I used the firstOrNew method but it creates a new entry instead of updating the old one.



via Hola

Advertisement