I need to do a validation so that the database does not write repeated data to a table, which is an intermediate table of a many to many relationship, not having an id. Below is the code.
public function invite(Event $event)
{
request()->validate([
'guest_ids'=>'required'
]);
if(Auth::user()->id!=$event->user_id){
return response(['error'=>'this event is not yours'],401);
}
$ids=explode(';',request()->guest_ids);
foreach ($ids as $id) {
if (request()->guest_id=request()->confirmed) {
return "ok";
} else {
# code...
}
$event->guests()->attach(Guest::find($id));
}
$event->guests;
return $event;
}
The table has the event_id, guest_id, and confirmed fields.