title: Undefined property: stdClass::$username (View: /home/annonces/resources/views/welcome.blade.php)
body: <p>I tried to show the name of the creator of the post but I have an error</p>
  
  <pre><code>@section('content')
  &lt;div class="container" id="results"&gt;
          &lt;div class="row justify-content-center"&gt;
          &lt;div class="col-md-12" style="display: flex; flex-flow: row wrap;"&gt;
  
      @foreach ($posts as $post)
      &lt;div class="col-md-3"&gt;
         &lt;a href="{{ route('posts.show', ['post' =&gt; $post-&gt;id]) }}"&gt; &lt;img src="{{ asset('storage') . '/' . $post-&gt;image }}" class="w-100"&gt;&lt;/a&gt;
        &lt;div class="card-body"&gt;
          &lt;h5 class="card-title"&gt;{{ $post-&gt;title }}&lt;/h5&gt;
          &lt;small&gt;{{ Carbon\Carbon::parse($post-&gt;created_at)-&gt;diffForHumans() }}&lt;/small&gt;
          &lt;span&gt;Publié par {{ $post-&gt;username }}&lt;/span&gt;
          &lt;p class="card-text"&gt;{{ $post-&gt;descriptionpost }}&lt;/p&gt;
          &lt;p class="card-text"&gt;{{ $post-&gt;price }}&lt;/p&gt;
          &lt;a href="{{ route('posts.show', ['post' =&gt; $post-&gt;id]) }}" class="btn btn-primary"&gt;Voir&lt;/a&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  @endforeach
  </code></pre>
  
  <p><strong>Post Model</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Post extends Model
  {
      protected $guarded = [];
  
      public function user()
      {
          return $this-&gt;belongsTo('App\User');
      }
  }
  </code></pre>
  
  <p><strong>User Model</strong> </p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Notifications\Notifiable;
  use Illuminate\Contracts\Auth\MustVerifyEmail;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  
  class User extends Authenticatable implements MustVerifyEmail
  {
      use Notifiable;
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'username', 'nom', 'prenom', 'adresse', 'ville', 'codepostale', 'datedenaissance','email', 'password',
      ];
  
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
          'password', 'remember_token',
      ];
  
      /**
       * The attributes that should be cast to native types.
       *
       * @var array
       */
      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 following()
      {
          return $this-&gt;belongsToMany('App\Profile');
      }
  
      public function posts()
      {
          return $this-&gt;hasMany('App\Post')-&gt;orderBy('created_at', 'DESC');
      }
  }
  </code></pre>
  
  <p>ProfileController</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use App\User;
  use Illuminate\Http\Request;
  use Intervention\Image\Facades\Image;
  
  class ProfileController extends Controller
  {
      public function show(User $user)
      {
          $follows = (auth()-&gt;user()) ? auth()-&gt;user()-&gt;following-&gt;contains($user-&gt;profile-&gt;id) : false;
  
          return view('profile.show', compact('user', 'follows'));
      }
  
      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>PostController</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use App\Http\Requests\Poststore;
  use App\Post;
  use Illuminate\Http\Request;
  use Intervention\Image\Facades\Image;
  use Illuminate\Support\Facades\DB;
  
  class PostController extends Controller
  {
  
  
      public function index()
      {
          $posts = DB::table('posts')-&gt;orderBy('created_at', 'DESC')-&gt;paginate(1000);
  
          return view('welcome',['posts'=&gt; $posts]);
      }
  
      public function create()
      {
          return view('posts.create');
      }
  
      public function store()
      {
          $data = request()-&gt;validate([
              'title' =&gt; ['required', 'string'],
              'image' =&gt; ['required', 'image'],
              'price' =&gt; ['required', 'integer'],
              'descriptionpost' =&gt; ['required', 'string']
          ]);
  
          $imagePath = request('image')-&gt;store('uploads', 'public');
  
          $image = Image::make(public_path("/storage/{$imagePath}"))-&gt;fit(1200, 1200);
          $image-&gt;save();
  
  
          auth()-&gt;user()-&gt;posts()-&gt;create([
              'title' =&gt; $data['title'],
              'descriptionpost' =&gt; $data['descriptionpost'],
              'price' =&gt; $data['price'],
              'image' =&gt; $imagePath
          ]);
  
          return redirect()-&gt;route('profile.show', ['user' =&gt; auth()-&gt;user() ]);
      }
  
      public function show(Post $post)
      {
          return view('posts.show', compact('post'));
      }
  
      public function search(Request $request)
      {
          $words = $request-&gt;words;
          $posts = DB::table('posts')-&gt;where('title', 'LIKE', '%$words%')-&gt;orWhere('descriptionpost', 'LIKE', '%$words%')-&gt;orderBy('created_at', 'DESC')-&gt;get();
  
          return response()-&gt;json(['success' =&gt; true, 'posts' =&gt; $posts]);
      }
  }
  </code></pre>
  
  <p>My error:</p>
  
  <blockquote>
    <p>Undefined property: stdClass::$username (View: /home/annonces/resources/views/welcome.blade.php)</p>
  </blockquote>
  
  <p>Its my model post and user. Does anyone have an idea on how to resolve this? I don't know where the problem is located.</p>