title: user profile manager with laravel7
body: <p>With laravel7, I want to allow each user to be able to modify their profile, but I can't. With my code when the connected user wants to modify his profile there is a message telling him that he is not authorized. Which is not normal. I do not know where my error lies. Please help me!</p>
  
  <p>Routes:</p>
  
  <pre><code>route::get('/profiles/{user}', 'ProfileController@show')-&gt;name('profiles.show');
  route::get('/profiles/{user}/edit', 'ProfileController@edit')-&gt;name('profiles.edit');
  route::patch('/profiles/{user}', 'ProfileController@update')-&gt;name('profiles.update');
  </code></pre>
  
  <p>My update view:</p>
  
  <pre><code>@extends('layouts.app')
  
  @section('content')
  &lt;div class="container"&gt;
      &lt;div class="row justify-content-center"&gt;
          &lt;div class="col-md-8"&gt;
              &lt;div class="card"&gt;
                  &lt;div class="card-header alert-primary"&gt;{{ __('Update my profile') }}&lt;/div&gt;
  
                  &lt;div class="card-body"&gt;
                      &lt;form method="POST" action="{{ route('profiles.update', ['user' =&gt; $user]) }}" &gt;
                          @csrf
                          @method('PATCH')
                          &lt;div class="form-group row"&gt;
                              &lt;label for="firstname" class="col-md-4 col-form-label text-md-right"&gt;{{ __('Firstname') }}&lt;/label&gt;
  
                              &lt;div class="col-md-6"&gt;
                                  &lt;input id="firstname" type="text" class="form-control @error('firstname') is-invalid @enderror" name="firstname" value="{{ old('firstname') ?? $user-&gt;firstname}}" required autocomplete="firstname" autofocus&gt;
  
                                  @error('firstname')
                                      &lt;span class="invalid-feedback" role="alert"&gt;
                                          &lt;strong&gt;{{ $message }}&lt;/strong&gt;
                                      &lt;/span&gt;
                                  @enderror
                              &lt;/div&gt;
                          &lt;/div&gt;
  
                          &lt;div class="form-group row"&gt;
                              &lt;label for="lastname" class="col-md-4 col-form-label text-md-right"&gt;{{ __('Lastname') }}&lt;/label&gt;
  
                              &lt;div class="col-md-6"&gt;
                                  &lt;input id="lastname" type="text" class="form-control @error('lastname') is-invalid @enderror" name="lastname" value="{{ old('lastname') ?? $user-&gt;lastname}}" required autocomplete="lastname" autofocus&gt;
  
                                  @error('lastname')
                                      &lt;span class="invalid-feedback" role="alert"&gt;
                                          &lt;strong&gt;{{ $message }}&lt;/strong&gt;
                                      &lt;/span&gt;
                                  @enderror
                              &lt;/div&gt;
                          &lt;/div&gt; 
  
                          &lt;div class="form-group row"&gt;
                              &lt;label for="role" class="col-md-4 col-form-label text-md-right"&gt;{{ __('Role') }}&lt;/label&gt;
  
                              &lt;div class="col-md-6"&gt;
                                  &lt;input id="role" type="text" class="form-control @error('role') is-invalid @enderror" name="role" value="{{ old('role') ?? $user-&gt;role}}" required autocomplete="role" autofocus&gt;
  
                                  @error('role')
                                      &lt;span class="invalid-feedback" role="alert"&gt;
                                          &lt;strong&gt;{{ $message }}&lt;/strong&gt;
                                      &lt;/span&gt;
                                  @enderror
                              &lt;/div&gt;
                          &lt;/div&gt;
  
                          &lt;div class="form-group row"&gt;
                              &lt;label for="phone" class="col-md-4 col-form-label text-md-right"&gt;{{ __('Phone') }}&lt;/label&gt;
  
                              &lt;div class="col-md-6"&gt;
                                  &lt;input id="phone" type="text" class="form-control @error('phone') is-invalid @enderror" name="phone" value="{{ old('phone') ?? $user-&gt;phone}}" required autocomplete="phone" autofocus&gt;
  
                                  @error('phone')
                                      &lt;span class="invalid-feedback" role="alert"&gt;
                                          &lt;strong&gt;{{ $message }}&lt;/strong&gt;
                                      &lt;/span&gt;
                                  @enderror
                              &lt;/div&gt;
                          &lt;/div&gt;
  
  
                          &lt;div class="form-group row"&gt;
                              &lt;label for="email" class="col-md-4 col-form-label text-md-right"&gt;{{ __('E-Mail Address') }}&lt;/label&gt;
  
                              &lt;div class="col-md-6"&gt;
                                  &lt;input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') ?? $user-&gt;email}}" required autocomplete="email"&gt;
  
                                  @error('email')
                                      &lt;span class="invalid-feedback" role="alert"&gt;
                                          &lt;strong&gt;{{ $message }}&lt;/strong&gt;
                                      &lt;/span&gt;
                                  @enderror
                              &lt;/div&gt;
                          &lt;/div&gt;
  
                          &lt;div class="form-group row"&gt;
                              &lt;label for="adress" class="col-md-4 col-form-label text-md-right"&gt;{{ __('Adress') }}&lt;/label&gt;
  
                              &lt;div class="col-md-6"&gt;
                                  &lt;input id="adress" type="text" class="form-control @error('adress') is-invalid @enderror" name="adress" value="{{ old('adress') ?? $user-&gt;adress}}" required autocomplete="adress" autofocus&gt;
  
                                  @error('adress')
                                      &lt;span class="invalid-feedback" role="alert"&gt;
                                          &lt;strong&gt;{{ $message }}&lt;/strong&gt;
                                      &lt;/span&gt;
                                  @enderror
                              &lt;/div&gt;
                          &lt;/div&gt;
  
                          &lt;div class="form-group row"&gt;
                              &lt;label for="profession" class="col-md-4 col-form-label text-md-right"&gt;{{ __('Profession') }}&lt;/label&gt;
  
                              &lt;div class="col-md-6"&gt;
                                  &lt;input id="profession" type="text" class="form-control @error('profession') is-invalid @enderror" name="profession" value="{{ old('profession') ?? $user-&gt;profession}}" required autocomplete="profession" autofocus&gt;
  
                                  @error('profession')
                                      &lt;span class="invalid-feedback" role="alert"&gt;
                                          &lt;strong&gt;{{ $message }}&lt;/strong&gt;
                                      &lt;/span&gt;
                                  @enderror
                              &lt;/div&gt;
                          &lt;/div&gt;
  
                          &lt;div class="form-group row"&gt;
                              &lt;label for="password" class="col-md-4 col-form-label text-md-right"&gt;{{ __('Password') }}&lt;/label&gt;
  
                              &lt;div class="col-md-6"&gt;
                                  &lt;input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password"&gt;
  
                                  @error('password')
                                      &lt;span class="invalid-feedback" role="alert"&gt;
                                          &lt;strong&gt;{{ $message }}&lt;/strong&gt;
                                      &lt;/span&gt;
                                  @enderror
                              &lt;/div&gt;
                          &lt;/div&gt;
  
                          &lt;div class="form-group row"&gt;
                              &lt;label for="password-confirm" class="col-md-4 col-form-label text-md-right"&gt;{{ __('Confirm Password') }}&lt;/label&gt;
  
                              &lt;div class="col-md-6"&gt;
                                  &lt;input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password"&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
  
                          &lt;div class="form-group row mb-0"&gt;
                              &lt;div class="col-md-6 offset-md-4"&gt;
                                  &lt;button type="submit" class="btn btn-primary"&gt;
                                      {{ __('Update') }}
                                  &lt;/button&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/form&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  @endsection
  </code></pre>
  
  <p>My controller:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use app\User;
  use Validator;
  use Illuminate\Foundation\Validation\ValidatesRequests;
  
  class ProfileController extends Controller
  {
  
      public function __construct()
  { 
          $this-&gt;middleware('auth'); 
  }
  
      public function store(Request $request)
      {
          //
      }
  
      /**
       * Display the specified resource.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function show(User $user)
      {
          // 
          return view ('profiles.show', compact ('user'));
      }
  
      /**
       * Show the form for editing the specified resource.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function edit(User $user)
      {
          // 
          $this-&gt;authorize('update', $user-&gt;profile); 
          return view ('profiles.edit', compact ('user')); 
      }
  
      /**
       * Update the specified resource in storage.
       *
       *  @param  \Illuminate\Http\Request  $request
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function update( User $user)
      {
              //
  
      $this-&gt;authorize ('update', $user-&gt;profile);
      $data = request()-&gt;validate ([ 
          'firstname'=&gt;'required',
          'Lastname'=&gt;'required',
          'phone' =&gt; 'required' ,
          'email' =&gt; 'required',
          'adress' =&gt; 'required',
          'profession' =&gt; 'required',
          'password' =&gt; 'required', 
      ]);
      auth()-&gt;$user-&gt;update($data);
      return redirect()-&gt;route('profiles.show', ['user'=&gt; $user]); 
  
      } 
  
  }
  </code></pre>
  
  <p>My model:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model; 
  
  class Profile extends Model
  {
      protected $guarded = []; 
  
  
      public function user(){
          return $this-&gt;belongsTo('App\User');
      }
  }
  </code></pre>
  
  <p>thanks</p>