Laravel - Repetitive code in store and update functions I am working on a system and I can see myself using repetitive code that does not look right. I want to clean up my code but I don't really know many ways to do so. I have 2 methods (store and update). They both look like this

store

public function store(Request $request)
    {
        $user = Auth::user();

        $validatedData = $request-&gt;validate([
            'street' =&gt; ['required', 'string'],
            'number' =&gt; ['required', 'string'],
            'city' =&gt; ['required', 'string'],
            'state' =&gt; ['required', 'string'],
            'postal_code' =&gt; ['required', 'string'],
            'country' =&gt; ['required', 'string'],
            'phone' =&gt; ['required', 'string']
        ]);

        $addresses = Address::all();

        $billing = 0;
        if($request-&gt;is_billing) {
            $billing = $request-&gt;is_billing;
            foreach($addresses as $address) {
                if($address-&gt;is_billing == 1) {
                    $address-&gt;is_billing = 0;
                    $address-&gt;save();
                }
            }
        }

        $address = Address::create([
            'user_id' =&gt; $user-&gt;id,
            'token' =&gt; Str::random(32),
            'street_name' =&gt; $request-&gt;street,
            'house_number' =&gt; $request-&gt;number,
            'postal_code' =&gt; $request-&gt;postal_code,
            'state' =&gt; $request-&gt;state,
            'city' =&gt; $request-&gt;city,
            'country_id' =&gt; $request-&gt;country,
            'phone' =&gt; $request-&gt;phone,
            'is_billing' =&gt; $billing
        ]);

        return redirect('/dashboard/user/' . $user-&gt;user_token . '/addresses');
    }


update

public function update(Request $request, $id)
    {
        $addresses = Address::all();
        $address = Address::where('token', $id)-&gt;firstOrFail();
        $user = Auth::user();

        $billing = 0;
        if($request-&gt;is_billing) {
            $billing = $request-&gt;is_billing;
            foreach($addresses as $item) {
                if($item-&gt;is_billing == 1) {
                    $item-&gt;is_billing = 0;
                    $item-&gt;save();
                }
            }
        }

        $address-&gt;user_id = $user-&gt;id;
        $address-&gt;street_name = $request-&gt;street;
        $address-&gt;house_number = $request-&gt;number;
        $address-&gt;postal_code = $request-&gt;postal_code;
        $address-&gt;state = $request-&gt;state;
        $address-&gt;city = $request-&gt;city;
        $address-&gt;country_id = $request-&gt;country;
        $address-&gt;phone = $request-&gt;phone;
        $address-&gt;is_billing = $billing;

        $address-&gt;save();

        return redirect('/dashboard/user/' . $user-&gt;user_token . '/addresses');
    }


Currently, the code looks messy and I have a feeling it can be done much more efficiently. Can someone give me tips on how to clean this up?
No need to get all data from database. Instead, only update those rows that need to be updated. Only if needed, you should work with database, avoid otherwise. Database I/O is biggest speed consumer in web applications and generally in PHP applications [when used]. https://laravel.com/docs/master/eloquent#updates

Check this one for store method, should work way faster:

public function store(Request $request)
{
    $user = Auth::user();

    $validatedData = $request-&gt;validate([
        'street' =&gt; ['required', 'string'],
        'number' =&gt; ['required', 'string'],
        'city' =&gt; ['required', 'string'],
        'state' =&gt; ['required', 'string'],
        'postal_code' =&gt; ['required', 'string'],
        'country' =&gt; ['required', 'string'],
        'phone' =&gt; ['required', 'string']
    ]);

    $billing = $request-&gt;is_billing ?? 0;
    if ($billing) {
        Address::where(['is_billing' =&gt; 1])-&gt;update(['is_billing' =&gt; 0]);
    }

    $address = Address::create([
        'user_id' =&gt; $user-&gt;id,
        'token' =&gt; Str::random(32),
        'street_name' =&gt; $request-&gt;street,
        'house_number' =&gt; $request-&gt;number,
        'postal_code' =&gt; $request-&gt;postal_code,
        'state' =&gt; $request-&gt;state,
        'city' =&gt; $request-&gt;city,
        'country_id' =&gt; $request-&gt;country,
        'phone' =&gt; $request-&gt;phone,
        'is_billing' =&gt; $billing
    ]);

    return redirect('/dashboard/user/' . $user-&gt;user_token . '/addresses');
}


Next, insted line $user = Auth::user(), you can work with policies ( https://laravel.com/docs/master/authorization ). In docs you can see PostPolicy how's been created and yours should be named AddressPolicy.
And also, you should move validation to request file created with let's say

php artisan make:request AddressStoreRequest


Again, in docs you will find how to set code there ( https://laravel.com/docs/master/validation#creating-form-requests ).
That is what you can do to release controller method of code and set those code blocks in their respective classes. Although your code (way I wrote it above avoiding unnecessary DB calls) will work the same even if you don't make separate classes for form validation or for authorization.

Use this code and make similar for update method.