0

I am getting an error message relating to my model relationship and have an identical section of my other app that works fine. I've looked through everything many times and cannot see any reason my model relationship should not work. When I try to access the model relationship in the view, I get "Trying to get property 'name' of non-object"

I have the following in my Controller:

    public function edit($id)
{
    //
    $plansubmission = PlanSubmission::find($id);

    $hradmins = PSPlanToHRAdminMapping::where('plan_submission_plan_id', $id)->get();

    return view('planbuilder.add-hr-administrators', compact('plansubmission', 'hradmins'));

I have the following in my View:

    <h3 class="py-3">Current HR Administrators with Online Access</h3>

<table class="table">
    <thead>
      <tr>
        <th scope="col">Name</th>
        <th scope="col">Email Address</th>
         </tr>
    </thead>
    <tbody>



     @foreach($hradmins as $hradmin)
            <tr>
                <td> {{$hradmin->hradmin->name }}</td>
                <td> {{$hradmin->hradmin->email }}</td>

    @endforeach
        </tr>

    </tbody>

    </table>

I have the following in my Model:

<?php

namespace App;

 use Illuminate\Database\Eloquent\Model;

class PSPlanToHRAdminMapping extends Model
{
//
protected $guarded = [];

public function hradmin()
{
    return $this->belongsTo('App\HRAdmin');
}

}

I have the following Migration:

    {
    Schema::create('p_s_plan_to_h_r_admin_mappings', function (Blueprint $table) {
        $table->unsignedBigInteger('plan_submission_plan_id');
        $table->unsignedBigInteger('h_r_admin_id');
        $table->timestamps();

        $table->foreign('plan_submission_plan_id')->references('id')->on('plan_submissions');
        $table->foreign('h_r_admin_id')->references('id')->on('h_r_admins');
    });
}
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Alexander
  • 270
  • 5
  • 19
  • On this line: `$hradmin->hradmin->name`, `$hradmin->hradmin` is returning `null`. And you can't access a property of `null`, as `null` doesn't have properties. – Tim Lewis Jan 02 '20 at 19:56
  • i thought that was it at first but i don't have any null fields in my table – Alexander Jan 02 '20 at 19:58
  • 1
    `non-nullable` columns in your database table still doesn't ensure that `$hradmin->hradmin` will not be `null`. If there's no associated data, i.e. `table_a.id` doesn't match `table_b.foreign_id`, that can still return `null`. `belongsTo()` can be `null` depending on the parent record you're accessing; never assume something exists; always check for it. – Tim Lewis Jan 02 '20 at 20:01
  • thanks, that is helpful, but I'm still not able to fully diagnose what is happening. When I use the optional helper ( {{optional($hradmin->hradmin)->name }} ), I no longer get an error message. But, the output on my screen now has the proper number of rows (corresponding to how many HR Admins there are), but those rows are blank and do not reveal data. – Alexander Jan 02 '20 at 20:07
  • I think you might have an issue with your relationship logic. To me, `p_s_plan_to_h_r_admin_mappings` seems like a `pivot` table, as it contains 2 `integer` columns. This to me says that it should be a `many-to-many` (`belongsToMany()`) relationship, and not a `one-to-one`/`one-to-many` (`belongsTo()`/`hasMany()`/`hasOne()`) See https://laravel.com/docs/6.x/eloquent-relationships#many-to-many for full details. – Tim Lewis Jan 02 '20 at 20:17
  • i thought about that, but that is not it either. It is a one-to-many relationship. Oddly, I have the exact same code (substituting names) for the 'advisor' section of my project and it works perfectly – Alexander Jan 02 '20 at 20:33
  • 1
    I mean yeah, the way you have it defined is a `one-to-many`, since you've got a model that directly queries the `pivot` table. I don't see why that's necessary, when you can just use Laravel's established logic to link `PlanSubmission` and `HRAdmin` via a `belongsToMany()`, but yeah. Generally, you don't have a model for the `pivot` table, as you shouldn't directly be manipulating it. The `attach()`, `detach()` and `sync()` methods for a `belongsToMany()` handle that for you. – Tim Lewis Jan 02 '20 at 20:37

1 Answers1

2

It's possible that for some records you don't have any record for hradmin relationship so you can try adding optional helper like so:

<td> {{optional($hradmin->hradmin)->name }}</td>
<td> {{optional($hradmin->hradmin)->email }}</td>

for example to avoid error

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291