feat(usermanagement): enhance user-branch relationship by syncing branches and adding timestamps
This commit is contained in:
@@ -217,11 +217,23 @@ class UsersController extends Controller
|
|||||||
|
|
||||||
if ($validated) {
|
if ($validated) {
|
||||||
$user = User::create($validated);
|
$user = User::create($validated);
|
||||||
|
|
||||||
if ($user) {
|
if ($user) {
|
||||||
if ($request->roles) {
|
if ($request->roles) {
|
||||||
$user->assignRole($request->roles);
|
$user->assignRole($request->roles);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$branches = $request->input('branches', []);
|
||||||
|
|
||||||
|
$user->branches()->sync($branches);
|
||||||
|
|
||||||
|
if (!empty($branches)) {
|
||||||
|
$firstBranchId = $branches[0];
|
||||||
|
|
||||||
|
$user->branch_id = $firstBranchId;
|
||||||
|
$user->save();
|
||||||
|
}
|
||||||
|
|
||||||
return redirect()->route('users.index')->with('success', 'User created successfully.');
|
return redirect()->route('users.index')->with('success', 'User created successfully.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,6 +110,7 @@ class User extends Authenticatable
|
|||||||
|
|
||||||
public function branches()
|
public function branches()
|
||||||
{
|
{
|
||||||
return $this->belongsToMany(Branch::class, 'user_branches', 'user_id', 'branch_id');
|
return $this->belongsToMany(Branch::class, 'user_branches', 'user_id', 'branch_id')
|
||||||
|
->withTimestamps();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ class UserBranch extends Model
|
|||||||
|
|
||||||
public function user()
|
public function user()
|
||||||
{
|
{
|
||||||
return $this->belongsTo(User::class);
|
return $this->belongsTo(User::class)
|
||||||
|
->withTimestamps();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function branch()
|
public function branch()
|
||||||
|
|||||||
Reference in New Issue
Block a user