laravel many to many insert data
$user = User::find(2);
$roleIds = [1, 2];
$user->roles()->attach($roleIds);
$user = User::find(3);
$roleIds = [1, 2];
$user->roles()->sync($roleIds);
laravel many to many insert data
$user = User::find(2);
$roleIds = [1, 2];
$user->roles()->attach($roleIds);
$user = User::find(3);
$roleIds = [1, 2];
$user->roles()->sync($roleIds);
add data one to many laravel
$employee = employee::find(1);
$salary1 = new Salary;
$salary->amount = '123456789';
$salary->payment_date = '15/07/2020';
$salary2 = new Salary;
$salary->amount = '123456789';
$salary->payment_date = '16/07/2020';
$employee = $employee->salary()->saveMany([$salary1, $salary2]);
add data to laravel many to many relationship
1. $user->roles()->attach($roleId);
2. you may also pass an array of additional data to be inserted
$user->roles()->attach($roleId, ['expires' => $expires]);
3. // Detach a single role from the user...
$user->roles()->detach($roleId);
4. // Detach all roles from the user...
$user->roles()->detach();
5. Any IDs that are not in the given array will be removed from the intermediate
table
$user->roles()->sync([1, 2, 3]);
6. If you need to update an existing row in your pivot table, you may use
updateExistingPivot method. This method accepts the pivot record foreign
key and an array of attributes to update:
$user->roles()->updateExistingPivot($roleId, $attributes);
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us