connect table
Hello
I have table Evenement, and i want update this table on my controller :
public function destroy(Participant $participant, ParticipantbyEvent $ParticipantbyEvent, Evenement $event)
{
.....
$idevent = $participant->evenement_id;
if ($goodnbrplace === "illimité")
{
$event -> nbrPlace = "illimité";
$event -> update(array('id' => $idevent));
}
if ($goodnbrplace != "illimité")
{
$goodnbrplacenoillimite = $goodnbrplace + 1;
$event -> nbrPlace = $goodnbrplacenoillimite;
$event -> update(array('id' => $idevent));
}
return redirect('/eventsub');
}
But idk why, when i try dd($event) i have no connection on my table Evenement...
Ty
You are not using update correctly.
Update will only change the fields that you specify in the braces.
If you want to set the nbrPlace field then you should use save()
$event->nbrPlace = $goodnbrplace + 1;
$event->save();
(also, remove pointless temporary variables)
I need to use method update if i want update my table no ?
i change my code like this :
$idevent = $participant->evenement_id;
$events = Evenement::where('id', $idevent) ->get();
$goodnbrplace = $events[0] -> nbrPlace;
if ($goodnbrplace === "illimité")
{
$events -> nbrPlace = "illimité";
$events -> update();
}
if ($goodnbrplace != "illimité")
{
$goodnbrplacenoillimite = $goodnbrplace + 1;
$events -> nbrPlace = $goodnbrplacenoillimite;
$events -> update();
}
But i have this error now :
Method Illuminate\Database\Eloquent\Collection::save does not exist.
ok that work i remplace -> get by -> first
update is for use where you don't have the model, but have the ID and the new data
Please mark answered if your problem is resolved
Please or to participate in this conversation.