How to add default values to a multiselect with a belongstomany relation?

I have a user-rental pivot table and when creating a new rental I would need to assign the users that are active, active = 1
4 Replies
Dan Harrin
Dan Harrin2y ago
maybe use an observer on the pivot model
H.Bilbao
H.BilbaoOP2y ago
Thank you Dan Darrin. I have tried but I can't get it to work with the created method when creating a new record, on the other hand, if I put the same code in udpated it does work when creating a new record.
public function created(Rental $rental): void
{
$users = User::actived()->get();

$rentalFromDate = Carbon::create($rental->from);
$rentalToDate = Carbon::create($rental->to);

foreach ($users as $user) {
$userJoinDate = Carbon::create($user->join_date);

if ($userJoinDate->lte($rentalFromDate) || $userJoinDate->between($rentalFromDate, $rentalToDate)) {
$rental->users()->attach($user->id);
Log::info('User ID: ' . $user->id . ' Rental ID: ' . $rental->id . ' From: ' . $rentalFromDate . ' To: ' . $rentalToDate . ' Join: ' . $userJoinDate);
}
else {
Log::error('User ID: ' . $user->id . ' Rental ID: ' . $rental->id . ' From: ' . $rentalFromDate . ' To: ' . $rentalToDate . ' Join: ' . $userJoinDate);
}
}
}
public function created(Rental $rental): void
{
$users = User::actived()->get();

$rentalFromDate = Carbon::create($rental->from);
$rentalToDate = Carbon::create($rental->to);

foreach ($users as $user) {
$userJoinDate = Carbon::create($user->join_date);

if ($userJoinDate->lte($rentalFromDate) || $userJoinDate->between($rentalFromDate, $rentalToDate)) {
$rental->users()->attach($user->id);
Log::info('User ID: ' . $user->id . ' Rental ID: ' . $rental->id . ' From: ' . $rentalFromDate . ' To: ' . $rentalToDate . ' Join: ' . $userJoinDate);
}
else {
Log::error('User ID: ' . $user->id . ' Rental ID: ' . $rental->id . ' From: ' . $rentalFromDate . ' To: ' . $rentalToDate . ' Join: ' . $userJoinDate);
}
}
}
I can see the log that is inside the if, but nothing save in DB
Dan Harrin
Dan Harrin2y ago
observer on the pivot model, not the related model because the pivot model should be listened to when something gets attached
H.Bilbao
H.BilbaoOP2y ago
Thanks, I understand you. But from the resource form I am not adding anything in the pivot table, only in the related. What I want is that when I add a new record in Rental, that rental automatically assigned in the pivot table to the active users. Maybe I am missing something, but I don't quite understand how to do it as you say, because if I put the observer in the pivot table it won't launch because I don't add anything, right? I active MySQL log and and I see that it does the insert but followed by a delete. but I don't have any delete configured:
4042 Prepare insert into `rental_user` (`user_id`, `rental_id`, `created_at`, `updated_at`) values (?, ?, ?, ?)
4042 Execute insert into `rental_user` (`user_id`, `rental_id`, `created_at`, `updated_at`) values (2, 50, '2023-03-29 23:27:58', '2023-03-29 23:27:58')
4042 Close stmt
4042 Prepare select * from `rental_user` where `rental_user`.`rental_id` = ?
4042 Execute select * from `rental_user` where `rental_user`.`rental_id` = 50
4042 Close stmt
4042 Prepare delete from `rental_user` where (`rental_id` = ? and `user_id` = ?)
4042 Execute delete from `rental_user` where (`rental_id` = 50 and `user_id` = 2)
4042 Close stmt
4042 Quit
4042 Prepare insert into `rental_user` (`user_id`, `rental_id`, `created_at`, `updated_at`) values (?, ?, ?, ?)
4042 Execute insert into `rental_user` (`user_id`, `rental_id`, `created_at`, `updated_at`) values (2, 50, '2023-03-29 23:27:58', '2023-03-29 23:27:58')
4042 Close stmt
4042 Prepare select * from `rental_user` where `rental_user`.`rental_id` = ?
4042 Execute select * from `rental_user` where `rental_user`.`rental_id` = 50
4042 Close stmt
4042 Prepare delete from `rental_user` where (`rental_id` = ? and `user_id` = ?)
4042 Execute delete from `rental_user` where (`rental_id` = 50 and `user_id` = 2)
4042 Close stmt
4042 Quit
I have found the "problem". In the Resource, I have a multiselect to add/edit users and it deleted the records once the observer added it. At the moment I have eliminated the multiselect until I find a solution.
Want results from more Discord servers?
Add your server