Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DaGenius's avatar

Can't login to Admin section on Server

Admin section works fine on my local machine but when I upload to my hosting, trying to access the admin backend using username and password combination returns 500 error. When I checked the log files, here is what I saw:

ERROR: Trying to get property of non-object (View: /../resources/views/admin/students.blade.php) {"userId":1,"exception":"[object] (ErrorException(code: 0): Trying to get property of non-object (View: /../resources/views/admin/students.blade.php) at /../app/Student.php:46, ErrorException(code: 0): Trying to get property of non-object at /../app/Student.php:46)

Here's what my students.blade.php looks like:

          @if(!empty($students))
            @foreach($students as $key => $value)
           
            <tr>
              <td>{{$key+1}}</td>
              <td>{{$value->surname . " " .$value->firstname . " " . $value->othername}}</td>
              <td>{{$value->getRegisteredBy($value->registeredBy_id)}}</td>
              <td>{{$value->getMother($value->id)}}</td>
              <td>{{$value->getFather($value->id)}}</td>
              <td>{{date_format(date_create(substr($value->created_at,0,10)),"d-m-Y")}}</td>
              <td><a href="{{url('/admin/viewStudent/'.$value->id)}}" class="btn btn-info btn-sm">View details</a></td>
            </tr> -->
            @endforeach
          @endif

          </tbody>

And the Student.php line 46:

public function getMother($id) {
        $parent = Mother::where('student_id', $id)->first();
        return $parent->name; // line 46
 }

I am not sure why everything works fine on my local machine but refuses to work on the hosting server. I will appreciate your help.

0 likes
17 replies
tisuchi's avatar

First of all, make sure $value->id return data.

Secondly, make sure, you have imported Mother model correctly with proper case. It might have case sensitivity.

Personal Note: Your code has N+1 issue that will slow down your page. You need to improve your code.

2 likes
DaGenius's avatar

@TISUCHI - Thank you @tisuchi, the thing is everything works fine on my local machine, I only have this issue on my hosting server

munazzil's avatar

In you function it will return only first record from database use get instead of first in below,

public function getMother($id) {
    $parent = Mother::where('student_id', $id)->get();
    return $parent->name; // line 46
    }
Snapey's avatar
Snapey
Best Answer
Level 122

A number of issues. First of all - you should install Laravel debugbar so that it is obvious to you that you have n+1 issues. Infact that you have MULTIPLE N+1 issues.

Then, you should learn about Eager Loading so that mother, father and registered_by are attributes of your student and not methods you need to call.

Next, you should learn about Carbon and how dates are automatically cast to Carbon objects which you can them immediately format.

Finally, your actual problem is because one of the students has no mother specified and your getMother function (and probably the others) does not allow for this possibility.

Your code is better written as;

    @foreach($students as $student)
    
    <tr>
        <td>{{ $loop->iteration }}</td>
        <td>{{ $student->surname . " " .$student->firstname . " " . $student->othername}}</td>
        <td>{{ $student->registeredBy->name ?? '--' }}</td>
        <td>{{ $student->mother->name ?? '--' }}</td>
        <td>{{ $student->father->name ?? '--' }}</td>
        <td>{{ $student->created_at->format("d-m-Y") }}</td>
        <td><a href="{{ url('/admin/viewStudent/'.$student->id) }}" class="btn btn-info btn-sm">View details</a></td>
    </tr>
    @endforeach

    </tbody>

and then in the controller

$students = Student::with('mother', 'father', 'registeredBy')->get();
DaGenius's avatar

@SNAPEY - I'm grateful for your answers. Will make the necessary corrections and give a report on how it goes. @tisuchi already mentioned the N+1 issue, to be candid I am just learning what it is. I am a beginner but I will read on how to resolve n+1 issues and about carbon too. Thanks once again.

DaGenius's avatar

@SNAPEY - So I tried your solution but I got another error...

[2019-05-07 08:10:30] local.ERROR: Call to undefined relationship [mother] on model [App\Student]. {"userId":1,"exception":"[object] (Illuminate\Database\Eloquent\RelationNotFoundException(code: 0): Call to undefined relationship [mother] on model [App\Student]. 
tisuchi's avatar

@DAGENIUS - Make sure you have defined mother relationship on Student model.

You may show your relationship code.

DaGenius's avatar

@TISUCHI - I do not have any relationship now. I will appreciate ideas about how I should do this.

Snapey's avatar

in Student model

public function mother() 
{
        return $this->belongsTo(Mother::class);
 }
DaGenius's avatar

@snapey @tisuchi Thanks for your help. Now I have updated my codes, established relationships where necessary but it outputs null for relationships so, that $student->mother->name, $student->father->name and $student-registeredBy->name all outputs null.

DaGenius's avatar

@SNAPEY - This is what the fathers and mothers table look like

public function up() 
    {
        Schema::create('fathers', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('profession');
            $table->string('office_address');
            $table->string('residential_address');
            $table->string('phone');
            $table->string('email');
            $table->string('photo');
            $table->integer('student_id')->unsigned();
            $table->timestamps();
            $table->foreign('student_id')->references('id')->on('students')->onDelete('cascade');
        });
    }

This is for registered_bies

public function up() [registered_bies]
    {
        Schema::create('registered_bies', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->string('relationship');
            $table->integer('user_id')->unsigned();
            $table->timestamps();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

        });
    }

And this for the students

public function up() [students]
    {
        Schema::create('students', function (Blueprint $table) {
            $table->increments('id');
            $table->string('surname');
            $table->string('firstname');
            $table->string('othername');
            $table->string('intending_class');
            $table->string('last_class');
            $table->string('last_school');
            $table->string('nationality');
            $table->text('reason_for_living');
            $table->string('date_of_birth');
            $table->string('sex');
            $table->string('special_needs');
            $table->string('special_needs_comment')->nullable();
            $table->string('photo');
            $table->integer('admission_fee');
            $table->string('reference_code');
            $table->integer('registeredBy_id')->unsigned();
            $table->timestamps();
            $table->foreign('registeredBy_id')->references('id')->on('registered_bies')->onDelete('cascade');
        });
    }
Snapey's avatar

looks ok. Are you loading mother, father in the controller?

DaGenius's avatar

@SNAPEY - I called

use App\Father; use App\Mother; use App\RegisteredBy; in my controller, is there something else I need to do?

Snapey's avatar

as I wrote earlier

$students = Student::with('mother', 'father', 'registeredBy')->get();

Please or to participate in this conversation.