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

HarryN's avatar

Validation on update

I am creating a edit form for bookings on my app.

I tried rewatching this video but I feel a tab more confused. https://laracasts.com/series/laravel-5-from-scratch/episodes/9

I remember seeing somewhere that you can pass your edit forms data into a function with the current db data and it will merge and overwrite changed values, I am guessing I should be using that for updating data?

Also, in order to make sure the update form fields are validated do I need to make a new request file? Or can it piggyback off of the main CreateBookingRequest.php

Here is my current update function


    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($slug, $id, Request $request)
    {
        $booking = Booking::with('company')->findOrFail($id);
        $booking->first_name = $request->get('first_name');
        $booking->save();
    }

p.s sorry if this post seems sloppy, its been a long day! :(

0 likes
4 replies
martinbean's avatar

@Haryke The Eloquent method you’re looking for is aptly named “update”. You can pass an array of attributes, and so long as they’re marked as fillable, will replace the values for that row in the database.

$model->update($attributes);

With regards to form requests, it’s up to you how to structure them. If you find you have the same validation for updating a record as you do creating one (or 90%) of the way there, then there’s nothing stopping you from extending the form request class for creation, and modifying it a little. For example:

class UpdateRequest extends CreateRequest {

    public function validate()
    {
        // Get create validation rules
        $rules = parent::rules();

        // Modify $rules array here

        return $rules;
    }
}

As a side-note, if you use route–model binding, you also don’t need to query models in your controller actions. Instead, a model instance can be injected right in there.

HarryN's avatar

@martinbean When I extend my CreateBookingRequest.php and have a rule such as a unqiue email. How will this handle that? Because the email wont be unique because the data already exists in the database, I just want to update it?

binalfew's avatar

@Haryke I agree with you on this one. But I think you can handle it like this:

$rules = [
    'name' => 'required|max:255'
    'email' => 'required|max:255'
];

if(Request::isMethod('post'))
{
    $rules['email'] = $rules['email'].'|unique:users';
}

Please or to participate in this conversation.