Level 102
If it does not have an advisor you need a fallback
{{ $student->advisers['advisername'] ?? '' }}
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
After deleting one adviser. this error shows "Trying to access array offset on value of type null". can you help me fix this. thank you.
Adviser model:
public function students()
{
return $this->hasMany(Student::class);
}
public function getAdviserNameAttribute()
{
return $this->first_name.' '.$this->last_name;
}
Student model:
public function advisers()
{
return $this->belongsTo(Adviser::class, 'adviser_id');
}
student migration:
$table->BigInteger('adviser_id')->unsigned()->nullable();
$table->foreign('adviser_id')->references('id')->on('advisers')->onDelete('set null');
index.blade.view:
<td>
{{ $student->advisers['advisername'] }}
</td>
controller:
public function index()
{
$students = Student::paginate(5);
$users = User::all();
$advisers = Adviser::all();
return view ('admin.index', compact('students', 'users', 'advisers'));
}
//delete adviser
public function deleteAdviser(Adviser $adviser)
{
$adviser->delete();
return redirect()->back()->with("error","Deleted!");
}
If it does not have an advisor you need a fallback
{{ $student->advisers['advisername'] ?? '' }}
Please or to participate in this conversation.