Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TomFromWindowsSupport's avatar

API instance gets created instead of editted

Hey all,

I'm designing an API and one of my routes is editing, the controller looks like this:

public function update(Request $request, Event $event, $id) {

        $request->validate([
            'id' => 'required',
            'title' => 'required|max:30',
            'description' => 'required|max:455',
            'place' => 'required',
            'time_happening' => 'date',
        ]);

        $event->updateFromArray($request->toArray());

        return response()->json($event);
    }

while the Model's updateFromArray looks like:

public function updateFromArray($data) {
        
        $this->id = $data['id'];
        $this->title = $data['title'];
        $this->description = $data['description'];
        $this->place = $data['place'];
        $this->time_happening = $data['time_happening'];
        $this->created_by = $data['created_by'];
        $this->group_id = $data['group_id'];
        $this->save();
        
        
        return $this;
    }

also the route in api.php:

Route::put('event/{id}', 'EventController@update');
  • If I don't enter the id on both function in controller and model, a new Event will be created.
  • If I enter both as shown above I get the following on Insomnia:
Method Illuminate\Validation\Validator::validate2 does not exist.

The Json body I'm entering looks like this:

{
            "id" : 2,
      "title": "New Updated Event in Paris.",
      "description": "Qui molestiae consectetur ipsam vel dicta. Nesciunt architecto laborum accusantium eos facere temporibus iure fugiat. Et dicta incidunt ut deleniti eum alias earum.",
      "place": "83224 Jordan Curve Suite 010",
      "time_happening": "2019-09-25 03:29:46",
            "created_by" : 5,
            "group_id" : 2
}

and the endpoint I'm calling: http://www.events-occursum.io/api/event/2

Thanks

0 likes
6 replies
bobbybouwmann's avatar

I think you're mixing up some stuff here. First of all you already have the id in the url. This means you don't have to add the $id parameter in the function. The route model binding with Event $event is already taking care of this!

public function update(Request $request, Event $event) 
{
    $request->validate([
        'id' => 'required',
        'title' => 'required|max:30',
        'description' => 'required|max:455',
        'place' => 'required',
        'time_happening' => 'date',
    ]);

    $event->updateFromArray($request->toArray());

    return response()->json($event);
}

Now if you look at the update method you do something strange here. You're also updating the id. You already have the event object with the id, so you don't have to do that.

public function updateFromArray($data) 
{
    $this->id = $data['id'];
    $this->title = $data['title'];
    $this->description = $data['description'];
    $this->place = $data['place'];
    $this->time_happening = $data['time_happening'];
    $this->created_by = $data['created_by'];
    $this->group_id = $data['group_id'];
    $this->save();

    return $this;
}

However you could even do this instead

public function updateFromRequest(Request $request)
{
    $this->update($request->except('id'));

    return $this;
} 

This will do the same thing as the other method above!

Snapey's avatar
Snapey
Best Answer
Level 122

If you want to route model bind the event, change the parameter in the route to event instead of id

Eg

Route::put('event/{event}', 'EventController@update');

At present your controller is being passed a new event instance AND an id

Change controller


public function update(Request $request, Event $event)

Please or to participate in this conversation.