jeudi 27 février 2020

Laravel 5.4 one belongs to relationship not working

Currently I'm trying to make a belongsTo relation work

I'm trying to show some data in a table like this:

                        <tr>
                            <td> <?php echo e($exam->id); ?> </td>
                            <td> <?php echo e($exam->name); ?> </td>
                            <td> <?php echo e(($exam->exam_type_on == 2) ? __('External') : __('Internal')); ?> </td>
                            <td> <?php echo e(($exam->groupsExams != null) ? $exam->groupsExams->pluck('name')->implode(',') : ''); ?> </td>
                            <td> <?php echo e($exam->createdBy->name); ?> </td>
                            <td> <?php echo e($exam->updatedBy->name); ?> </td>
                            <td align="center">
                                <a href="<?php echo e(url('exams/'.$exam->id.'/edit/')); ?>"><i class="fa fa-pencil"></i></a>
                                &nbsp;&nbsp;<a class="remove-obj" data-action="<?php echo e(route('exams.destroy', [ 'id' => $exam->id])); ?>"  data-title="<?php echo app('translator')->getFromJson('Are you sure to delete this exam?'); ?>" data-true="<?php echo app('translator')->getFromJson('yes, delete'); ?>" data-false="<?php echo app('translator')->getFromJson('No, cancel please!'); ?>" data-deleted="<?php echo app('translator')->getFromJson('Deleted!'); ?>" data-text="<?php echo app('translator')->getFromJson('You will not be able to recover this exam!'); ?>" data-success-message="<?php echo app('translator')->getFromJson('Exam has been deleted.'); ?>"><i class="fa fa-trash"></i></a>
                            </td>
                        </tr>

But I keep getting this error:

Trying to get property 'name' of non-object

This is the model

public function createdBy() {
    return $this->belongsTo(User::class, 'created_by');
}

public function updatedBy() {
    return $this->belongsTo(User::class, 'updated_by');
}

The error is at the line with createdBy->name but updatedBy seems to work. I don't know what's wrong with the code



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire