Martins
Martins

Reputation: 45

Laravel relation belongsTo error Trying to get property 'name' of non-object

I'm getting the error Trying to get property 'name' of non-object when trying to print in my view the attribute name of a belongsTo relationship made to the entity (model) Institution, the funniest thing is that I did the same thing with the user relationship and it worked.

I have tried several resolutions that have in the stack overflow but none of them got results, seems to be simple thing, but ... don't work

Model Institution

class Institution extends Model implements Transformable
{

    use TransformableTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name'];

    public $timestamps = true;
}

Model User

class User extends Authenticatable
{
    use Notifiable;
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

    public $timestamps = true;

    protected $table = 'users';

    protected $fillable = [
        'cpf',
        'name',
        'phone',
        'birth',
        'gender',
        'notes',
        'email',
        'password',
        'status',
        'permission'
    ];

Group Entity

class Group extends Model implements Transformable
{
    use TransformableTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name','user_id','institution_id'];

    // a classe grupo pertence ao usuario atraves do metodo owneruser e institution
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function institution()
    {
        return $this->belongsTo(Institution::class);
    }

}

GroupsController

    public function index()
    {

        $groups = $this->repository->all();
        $user_list = $this->userRepository->selectBoxList();
        $institution_list = $this->institutionRepository->selectBoxList();

        return view('group.index', [
            'groups' => $groups,
            'user_list' => $user_list,
            'institution_list' => $institution_list
        ]);
    }

View Group

@foreach($groups as $group)
<tr>
    <td>{{ $group->id }} </td>
    <td>{{ $group->name }} </td>

    // In this line where the error
    // Trying to get property 'name' of non-object  occurs
    <td> {{$group->institution->name }} </td>
    // This line is working
    <td> {{ $group->user->name }} </td>

    <td>
        {!! Form::open(['route' => ['group.destroy', $group->id], 'method' => 'delete']) !!}
            {!! Form::submit("Remover") !!}
        {!! Form::close() !!}
    </td>
</tr>
@endforeach

I hope the output in <td>{{ $group-> institution-> name }}</ td> Be the name of the institution in which the group is associated through the foreign key, instead it returns the error.

Upvotes: 1

Views: 100

Answers (1)

nakov
nakov

Reputation: 14278

From the DB image I see that you are using uppercase for your foreign key Institution_id while using it lowercase elsewhere. I believe it is case-sensitive, so try this in your Group model:

public function institution()
{
    return $this->belongsTo(Institution::class, 'Institution_id');
}

And change it in the fillable list as well.

Upvotes: 1

Related Questions