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

joshsalway's avatar

How to restore using eloquent, with an api with react? With softdeletes

     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $customer = Customer::withTrashed()->findOrFail($id);

        if($customer->deleted_at != null) {
            $customer->restore();
            return response()->json($customer);
        }

        $customer->first_name = $request->first_name;
        $customer->last_name = $request->last_name;
        $customer->email = $request->email;
        $customer->save();
        return response()->json($request);
    }

How do I restore a customer? If I'm using an API resource...

With SoftDeletes a deleted_at column is created and only a date is added...

This works is there any way you would improve this?

https://laravel.com/docs/7.x/eloquent#querying-soft-deleted-models

0 likes
1 reply
Brian Kidd's avatar

@joshsalway This is personal preference as to how to handle it. I agree I don’t particularly like a method doing multiple things. You could add another method to your current controller for restoring soft deleted models or another option is to create a CustomerRestoreController and have the update method there for explicitly restoring the customer.

Please or to participate in this conversation.