title: Laravel 5.8 display description and change picture profile
body: <p>I try to create/modify a profile, but when I want to add an image in profile, it doesn't work and doesn't load into the database.</p>
  
  <p><strong>ProfileController</strong> </p>
  
  <pre class="lang-php prettyprint-override"><code>class ProfileController extends Controller
  {
      public function show(User $user)
      {
          return view('profile.show', compact('user'));
      }
  
      public function edit(User $user)
      {
          $this-&gt;authorize('update', $user-&gt;profile);
          return view('profile.edit', compact('user'));
      }
  
      public function update(User $user)
      {
          $this-&gt;authorize('update', $user-&gt;profile);
          $data = request()-&gt;validate([
              'description' =&gt; 'required',
              'image' =&gt; 'sometimes|image|max:3000'
          ]);
  
          if (request('image')) {
              $imagePath = request('image')-&gt;store('avatars', 'public');
  
              $image = Image::make(public_path("/storage/{$imagePath}"))-&gt;fit(800, 800);
              $image-&gt;save();
  
              auth()-&gt;user()-&gt;profile-&gt;update(array_merge($data,
                  ['image' =&gt; $imagePath]
              ));
          } else {
              auth()-&gt;user()-&gt;profile-&gt;update($data);
          }
  
          auth()-&gt;user()-&gt;profile-&gt;update($data);
  
          return redirect()-&gt;route('profile.show', ['user' =&gt; $user]);
      }
  }
  </code></pre>
  
  <p><strong>User</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>class User extends Authenticatable implements MustVerifyEmail
  {
      use Notifiable;
  
      protected $fillable = [
          'username', 'nom', 'prenom', 'adresse', 'ville',
          'codepostale', 'datedenaissance', 'email', 'password',
      ];
  
  
      protected $hidden = [
          'password', 'remember_token',
      ];
  
      protected $casts = [
          'email_verified_at' =&gt; 'datetime',
      ];
  
      protected static function boot()
      {
          parent::boot();
          static::created(function ($user) {
              $user-&gt;profile()-&gt;create([
                  'description' =&gt; $user-&gt;username
              ]);
          });
      }
  
      public function getRouteKeyName()
      {
          return 'username';
      }
  
      public function profile()
      {
          return $this-&gt;hasOne('App\Profile');
      }
  
      public function posts()
      {
          return $this-&gt;hasMany('App\Post')-&gt;orderBy('created_at', 'DESC');
      }
  }
  </code></pre>
  
  <p><strong>show.blade.php</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>@extends('layouts.app')
  @section('content')
      &lt;div class="container"&gt;
          &lt;div class="row"&gt;
              &lt;div class="col-4"&gt;
                  &lt;img src="{{ $user-&gt;profile-&gt;getImage() }}" class="rounded-circle w-100" alt="" style="max-width: 240px;"&gt;
              &lt;/div&gt;
              &lt;div class="col-8"&gt;
                  &lt;div class="d-flex align-items-baseline"&gt;
                      &lt;div class="h4 mr-3 pt-2"&gt;{{ $user-&gt;username }}&lt;/div&gt;
                      &lt;button class="btn btn-primary"&gt;S'abonner&lt;/button&gt;
                  &lt;/div&gt;
                  &lt;div class="d-flex"&gt;
                      &lt;div class="mr-3"&gt;{{ $user-&gt;posts-&gt;count() }} article(s) en vente
                      &lt;/div&gt;
                      @can('update', $user-&gt;profile)
                          &lt;a href=" {{ route('profile.edit', ['username' =&gt; $user-&gt;username]) }}"&gt;Modifier Profile&lt;/a&gt;
                      @endcan
                      &lt;div class="mt-3"&gt;
                          &lt;div class="font-weight-bold"&gt;
                              {{ $user-&gt;profile-&gt;title }}
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="row mt-5"&gt;
                  @foreach ($user-&gt;posts as $post)
                      &lt;div class="col-4"&gt;
                          &lt;a href="{{ route('posts.show', ['post' =&gt; $post-&gt;id]) }}"&gt;
                              &lt;img src="{{ asset('storage') . '/' . $post-&gt;image }}" alt="" class="w-100"&gt;
                          &lt;/a&gt;
                      &lt;/div&gt;
                  @endforeach
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  @endsection
  </code></pre>
  
  <p>edit.blade.php</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"&gt;Modifier profile&lt;/div&gt;
  
                  &lt;div class="card-body"&gt;
                      &lt;form method="POST" action="{{ route('profile.update', ['user' =&gt; $user]) }}" enctype="multipart/form-data"&gt;
                          @csrf
                          @method('PATCH')
  
                          &lt;div class="form-group"&gt;
                              &lt;label for="description"&gt;Description&lt;/label&gt;
  
                              &lt;div class="col-md-6"&gt;
                                  &lt;textarea id="description" type="text" class="form-control @error('description') is-invalid @enderror" name="description" autocomplete="description" autofocus&gt;{{ old('description') ?? $user-&gt;profile-&gt;description }}&lt;/textarea&gt;
  
                                  @error('description')
                                      &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"&gt;    
                              &lt;div class="custom-file"&gt;
  
                                  &lt;input type="file" name="image" class="custom-file-input @error('image') is-invalid @enderror" id="validatedCustomFile" &gt;
                                  &lt;label class="custom-file-label" for="validatedCustomFile"&gt;Choisir une image&lt;/label&gt;
                                  @error('image')
                                      &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="col-md-6 offset-md-4"&gt;
                                  &lt;button type="submit" class="btn btn-primary"&gt;
                                      Modifier profile
                                  &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><strong>Profile.php</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>class Profile extends Model
  {
      protected $fillable = ['description']; 
  
      public function user()
      {
          return $this-&gt;belongsTo('App\User');
      }
  
      public function getImage()
      {
          $imagePath = $this-&gt;image ?? 'avatars/default.png';
  
          return "/storage/" . $imagePath;
      }
  }
  </code></pre>
  
  <p><strong>create_profiles_tables.php</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>class CreateProfilesTable extends Migration
  {
      public function up()
      {
          Schema::create('profiles', function (Blueprint $table) {
              $table-&gt;bigIncrements('id');
              $table-&gt;text('description')-&gt;nullable();
              $table-&gt;string('image')-&gt;nullable();
              $table-&gt;unsignedBigInteger('user_id')-&gt;index();
              $table-&gt;timestamps();
          });
      }
  
      public function down()
      {
          Schema::dropIfExists('profiles');
      }
  }
  </code></pre>
  
  <p>I don't know what problem for uploads pictures in the database and change profile picture, can someone help?</p>