Laravel 5.8: Trying to get property ‘created_at’ of non-object

  laravel, laravel-5.8, php

I’m using Laravel 5.8 to develop my project and in this project, I have a OneToMany relationship between Member Model & Student Model like this:

Student.php:

public function member()
    {
        return $this->belongsTo(Student::class, 'std_mbr_id');
    }

Member.php:

public function student()
    {
        return $this->hasOne(Student::class,'std_mbr_id');
    }

Then at the Controller, I added this:

public function index()
    {
        $student = new Student();
        $students = $student->searchStudents()->paginate(20);
        $custom = new Student();
        $customs = $custom->all();
        return view('admin.students.custom', compact('students','customs'));
    }

Now at the view I want to get created_at timestamp like this:

$customs->find($student->mbr_id)->created_at

But this will return this error:

Trying to get property ‘created_at’ of non-object

However when I do $customs->find($student->mbr_id), I can properly get the timestamps:

enter image description here

So to access the created_at timestamp in this situation?

Source: Laravel

Leave a Reply