Hello! Created relationships in laravel, and everything works until null values ​​are met, help me figure it out, thanks in advance!

User model:

public function issues() { return $this->hasMany('App\Issues', 'worker'); } 

Model Issues:

 public function user() { return $this->belongsTo('App\User', 'worker'); } 

Issues table:

  Schema::table('issues', function (Blueprint $table) { $table->integer('worker')->nullable()->unsigned(); $table->foreign('worker')->references('id')->on('users')->onDelete('set null')->onUpdate('cascade'); }); 

Representation:

  @foreach($issues as $issue) <tr> <td>{{ $issue->title }}</td> <td>{{ $issue->user->name }}</td> </tr> @endforeach 

As long as all foreign keys are specified, everything works, as null is encountered, then we get the error Trying to get the property of non-object , google, but something is not there.

  • Well, what do you expect to see? You have no ligament, i.e. the relation returns null, and then you try to take from null the name {{ $issue->user ? $issue->user->name:'' }} {{ $issue->user ? $issue->user->name:'' }} - Orange_shadow
  • Or rather, there is a bunch, but the null has no name ... yes, that's right .... eh. Thank you!) - noanother

0