skip to Main Content

I am trying to make a profile settings page where people can chane name email and password and only the password gets updated but all the rest remain the same. I cant understand why below i have the code
Note i use breeze

settings.blade.php

<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Settings') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 bg-white border-b border-gray-200">
                    <!-- Personal Details heading -->
                    <div class="mb-4">
                        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                            {{ __('Personal Details') }}
                        </h2>
                    </div>
                    <!-- Show id with prefix-->
                    <div class="mb-4">
                        <x-label for="id" :value="__('ID')" />
                        <x-input id="id" class="block mt-1 w-full" type="text" name="id" :value="Auth::user()->id + 243254" disabled />
                    </div>
                    <form method="POST" action="{{ route('settings.update')}}">
                        @method('PUT')
                        @csrf
                        <div class="grid grid-cols-2 gap-6">
                            <div class="grid grid-rows-2 gap-6">
                                <div>
                                    <x-label for="name" :value="__('Name')" />
                                    <x-input id="name" class="block mt-1 w-full" type="text" name="name" value="{{ auth()->user()->name }}" autofocus />
                                </div>
                                <br />
                                <div>
                                    <x-label for="email" :value="__('Email')" />
                                    <x-input id="email" class="block mt-1 w-full" type="email" name="email" value="{{ auth()->user()->email }}" autofocus />
                                </div>
                            </div>
                            <br />
                            <div class="grid grid-rows-2 gap-6">
                                <div>
                                    <x-label for="new_password" :value="__('New password')" />
                                    <x-input id="new_password" class="block mt-1 w-full"
                                             type="password"
                                             name="password"
                                             autocomplete="new-password" />
                                </div>
                                <br />
                                <div>
                                    <x-label for="confirm_password" :value="__('Confirm password')" />
                                    <x-input id="confirm_password" class="block mt-1 w-full"
                                             type="password"
                                             name="password_confirmation"
                                             autocomplete="confirm-password" />
                                </div>
                            </div>
                        </div>
                        <div class="flex items-center justify-end mt-4">
                            <x-button class="ml-3">
                                {{ __('Update') }}
                            </x-button>
                        </div>
                    </form>
                    <!-- Address History heading -->
                    <div class="mt-4">
                        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                            {{ __('Address History') }}
                        </h2>
                    </div>
                    <br />
                    <form method="POST" action="#">
                        @csrf
                        <!-- Previous Addresses History Table using tr -->
                        <table>
                            <tr>
                                <th>Address</th>
                                <th>Postcode</th>
                                <th>City</th>
                                <th>Country</th>
                                <th>From</th>
                                <th>To</th>
                            </tr>
                        </table>

                        <br />
                        <!--Add heading to add new address-->
                        <h3 class="font-semibold text-xl text-gray-800 leading-tight">
                            {{ __('Add new address') }}
                        </h3>
                        <br />
                        <!-- New Address form-->
                        <div class="grid grid-cols-2 gap-6">
                            <div class="grid grid-rows-2 gap-6">
                                <div>
                                    <x-label for="address" :value="__('Address')" />
                                    <x-input id="address" class="block mt-1 w-full" type="text" name="address" value="{{ auth()->user()->address }}" autofocus />
                                </div>
                                <br />
                                <div>
                                    <x-label for="city" :value="__('City')" />
                                    <x-input id="city" class="block mt-1 w-full" type="text" name="city" value="{{ auth()->user()->city }}" autofocus />
                                </div>
                            </div>
                            <br />
                            <div class="grid grid-rows-2 gap-6">
                                <div>
                                    <x-label for="state" :value="__('State')" />
                                    <x-input id="state" class="block mt-1 w-full" type="text" name="state" value="{{ auth()->user()->state }}" autofocus />
                                </div>
                                <br />
                                <div>
                                    <x-label for="zip" :value="__('Zip')" />
                                    <x-input id="zip" class="block mt-1 w-full" type="text" name="zip" value="{{ auth()->user()->zip }}" autofocus />
                                </div>
                            </div><br />
                            <!-- add from to which day living in this address-->
                            <div class="grid grid-rows-2 gap-6">
                                <div>
                                    <x-label for="from" :value="__('From')" />
                                    <x-input id="from" class="block mt-1 w-full" type="date" name="from" value="{{ auth()->user()->from }}" autofocus />
                                </div>
                                <br />
                                <div>
                                    <x-label for="to" :value="__('To')" />
                                    <x-input id="to" class="block mt-1 w-full" type="date" name="to" value="{{ auth()->user()->to }}" autofocus />
                                </div>
                            </div>



                        <div class="flex items-center justify-end mt-4">
                            <x-button class="ml-3">
                                {{ __('Add Address') }}
                            </x-button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>

SettingsController.php

<?php

namespace AppHttpControllers;

use AppHttpRequestsUpdateSettingsRequest;
use IlluminateHttpRequest;


class SettingsController extends Controller
{
    public function update(UpdateSettingsRequest $request){
        auth()->user()->update($request->only('name', 'email'));
        if ($request->input('password')){
            auth()->user()->update([
                'password' => bcrypt($request->input('password'))
            ]);

        }
        return redirect()->route('settings')->with('message', 'Settings updated');
    }
}

web.php

<?php

use IlluminateSupportFacadesRoute;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});




Route::group(['middleware' => 'auth'], function(){
    Route::get('/dashboard', function () {
        return view('dashboard');
    })->name('dashboard');

    Route::view('settings', 'settings')->name('settings');
    Route::view('creditcards', 'creditcards')->name('creditcards');
    Route::view('loans', 'loans')->name('loans');
    Route::put('settings', [AppHttpControllersSettingsController::class, 'update'])
        ->name('settings.update');




});




require __DIR__.'/auth.php';

anyone knows why?

I tried to do ifs, to take out the values on the form and still

2

Answers


  1. Just get a User model instance first, then update it

    <?php
    
    namespace AppHttpControllers;
    
    use AppHttpRequestsUpdateSettingsRequest;
    use IlluminateHttpRequest;
    
    
    class SettingsController extends Controller
    {
        public function update(UpdateSettingsRequest $request){
            $user = User::findOrFail(auth()->user()->id);
    
            $user->update($request->only('name', 'email'));
            if ($request->input('password')){
                $user->update([
                    'password' => bcrypt($request->input('password'))
                ]);
    
            }
            return redirect()->route('settings')->with('message', 'Settings updated');
        }
    }
    
    

    BTW, your code is not pretty effective. Your are hitting the database twice because updates.

    Instead of that, you should handle the password encrypt by using a mutator https://laravel.com/docs/9.x/eloquent-mutators#defining-a-mutator and Mass Assignment https://laravel.com/docs/9.x/eloquent#mass-assignment-json-columns

    Said that, your model could have something like:

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
      'name',
      'email',
      'password',
    ];
    
    /**
    * Interact with the user's password.
    *
    * @return IlluminateDatabaseEloquentCastsAttribute
    */
    protected function password(): Attribute
    {
        return Attribute::make(
          get: fn ($value) => $value,
          set: fn ($value) => bcrypt($value),
        );
    }
    

    Then in your controller just few lines:

    <?php
    
    namespace AppHttpControllers;
    
    use AppHttpRequestsUpdateSettingsRequest;
    use IlluminateHttpRequest;
    
    
    class SettingsController extends Controller
    {
        public function update(UpdateSettingsRequest $request){
            $user = User::findOrFail(auth()->user()->id);
            $user->update($request->all());
    
            return redirect()->route('settings')->with('message', 'Settings updated');
        }
    }
    
    
    Login or Signup to reply.
  2. Check your User model, maybe the other fields are not in the fillable property

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search