title: How can I get the user name who posted the post (laravel 5.8)
body: <p>I'm try to get the user name who create the post witch is <em><strong>(Problem model)</strong></em>, I do not know what is wrong I have try <a href="https://laravel.com/docs/5.8/eloquent-relationships#eager-loading" rel="nofollow noreferrer">Eager Loading</a> in laravel documentation and I have check these <a href="https://stackoverflow.com/questions/55319161/how-to-get-a-user-from-post-table-when-using-hasmany-relationship">Questions 1</a>  <a href="https://stackoverflow.com/questions/57596231/how-can-i-retreive-the-user-who-posted-the-post">Questions 2</a> and still get null or <code>Trying to get property 'name' of non-object</code> if I use <code>dd( $problem-&gt;user-&gt;name);</code></p>
  <p><strong>I use laravel 5.8</strong></p>
  <h1>ProblemsController.php</h1>
  <pre><code>namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\User;
  use App\Problems ;
  use App\Rules\Checkbox;
  use Validator;
  [...]
   public function index() 
      {
          //$users_row_num = User::count();
         // $problems_row_num = Problems::count();
  
         $problems = Problems::all();
         
  
          foreach ($problems as $problem) {
          /* Here should get name to send with view but I get null
           * if I try  $problem-&gt;user-&gt;name I get Trying to get property 'name' of non-object because user is null 
           */
              dd( $problem-&gt;user); 
          }
         
          return view('problems.index', [
              'problems' =&gt; $problem,
              'user_numder' =&gt; $users_row_num,
              'problem_number' =&gt; $problems_row_num,
          ]);
      }
  
  [...]
  </code></pre>
  <h1>Problems.php (model)</h1>
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Problems extends Model
  {
      
      public function user()
      {
          return $this-&gt;belongsTo('App\User');
      }
  }
  
  </code></pre>
  <h1>Usre.php (model)</h1>
  <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
  {
  [...]
  
   public function problem()
      {
          return $this-&gt;hasMany('App\Problems');
      }
  
  [...]
  }
  
  </code></pre>
  <h1>index.blade.php</h1>
  <pre><code> @foreach ($problems as $problem)
                              &lt;tr&gt;
                                  &lt;td&gt;{{ $problem-&gt;accountNumber }}&lt;/td&gt;
                                  &lt;td&gt;{{ $problem-&gt;accountName }}&lt;/td&gt;
                                  &lt;td&gt;{{ $problem-&gt;accountEmail }}&lt;/td&gt;
                                  &lt;td&gt;{{ $problem-&gt;date }}&lt;/td&gt;
                                  &lt;td&gt;{{ $problem-&gt;problem }}&lt;/td&gt;
                                 
                                  &lt;td&gt;{{ $problem-&gt;addedBy }}&lt;/td&gt; &lt;!-- Here I get user id (foreign key) I want to get name of that user --&gt;
                                 
                                  &lt;td&gt;{{ $problem-&gt;comment }}&lt;/td&gt;
                                  &lt;td&gt;{{ $problem-&gt;solvedBy }}&lt;/td&gt;
                                  &lt;td&gt;{{ $problem-&gt;solved }}&lt;/td&gt;
                                  &lt;td&gt;{{ $problem-&gt;created_at-&gt;format('d/m/Y H:i') }}&lt;/td&gt;
                                  &lt;td class=&quot;text-right&quot;&gt; &lt;/td&gt;
                              &lt;/tr&gt;
                              @endforeach
  </code></pre>
  <h1>Problem table</h1>
  <pre><code>[...]
   public function up()
      {
          Schema::create('problems', function (Blueprint $table) {
              $table-&gt;bigIncrements('id');
              $table-&gt;integer('accountNumber');
              $table-&gt;string('accountName');
              $table-&gt;string('accountEmail');
              $table-&gt;date('date');
              $table-&gt;longText('problem');
              $table-&gt;bigInteger('addedBy')-&gt;unsigned()-&gt;nullable();
              $table-&gt;longText('comment')-&gt;nullable();
              $table-&gt;string('solvedBy')-&gt;nullable();
              $table-&gt;boolean('solved');
              $table-&gt;timestamps();
          });
      }
  [...]
  
  </code></pre>
  <h1>ForingkeyAddedby migration</h1>
  <pre><code>[...]
  public function up()
      {
          Schema::table('problems', function(Blueprint $table){
              $table-&gt;foreign('addedBy')-&gt;references('id')-&gt;on('users')-&gt;onDelete('set null')-&gt;onUpdate('CASCADE');
  
          });
      }
  [...]
  
  </code></pre>
  <p><strong>I want to use <code>$problem-&gt;user-&gt;name</code> to get name of user who create post to display it in table</strong></p>
  <p><strong>Please help.</strong></p>
  <p><em><strong>THANKS ALL.</strong></em></p>