Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

388
Vistas
Laravel 5.8: Trying to get property 'created_at' of non-object

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 dd($customs->find($student->mbr_id)), I can properly get the timestamps:

enter image description here

So how to access the created_at timestamp in this situation?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

There are many possibilities to throw `non-object errors.

First of all, make sure you have added the primary key in the Student Model.

protected $primaryKey = 'std_id';

Second is if you passed wrong $student->mbr_id in the find() method and directly access specific columns it will throw an error:

Please check in your blade file that record exist or not:

@if($customs->find($student->smbr_id))
    {{ $customs->find($student->smbr_id)->created_at }}
@endif

I hope you will understand and solved it.

over 4 years ago · Santiago Trujillo Denunciar

0

Why not leverage the Eloquent dynamic relationship property (See https://laravel.com/docs/8.x/eloquent-relationships#one-to-many-inverse) and use

$student->member->created_at
over 4 years ago · Santiago Trujillo Denunciar

0

find() works with the primary key, which is std_id in this case. So to work with another field, you should do

$customs->where('std_mbr_id', $student->mbr_id)->first()->created_at
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda