Here is an AI generated answer, so you can get inspired, seems pretty ok to me:
To achieve this, you can introduce a new table (let's call it request_actions) in your database to keep track of whether a request has been acted upon or not. This table can have fields like user_id, accountant_id and status.
When a request is made, an entry is created in this table with the status as 'pending'. When either the 'accept' or 'reject' action is performed, the status is updated to 'used'.
Before performing any action, you can check the status of the request. If it's 'used', you can prevent the action and return a message to the user.
Here is how you can modify your acceptRequest method:
public function acceptRequest($userId, $accountantId)
{
$requestAction = RequestAction::where('user_id', $userId)
->where('accountant_id', $accountantId)
->first();
if ($requestAction && $requestAction->status == 'used') {
return redirect('/')->with('error_message', 'This request has already been acted upon.');
}
$accountant = User::findOrFail($accountantId);
$user = User::findOrFail($userId);
$accountant->users()->attach($userId);
// Update the request action status to 'used'
if ($requestAction) {
$requestAction->status = 'used';
$requestAction->save();
}
$data = [
'email' => '[email protected]',
'name' => 'test name',
'accountantName' => $accountant->name,
'userName' => $user->name,
];
Mail::to($accountant->email)->send(new Email($data, 'accept-accountant-request'));
return redirect('/')->with('success_message', 'Success');
}
You would need to do a similar check in your rejectRequest method as well.
Please note that you would need to create a new model RequestAction and a migration for the request_actions table. Also, don't forget to create an entry in the request_actions table when a new request is made.