0
votes

I have 4 tables (users, ask_questions, respones, categories). I'm trying to show each response done asked question but i'm getting this error: Trying to get property 'name' of non-object (View: C:\xampp\htdocs\COMPLIANCE-FAQ\resources\views\respones\show.blade.php)

the model respone.php

{
    public $table = 'respones';

    protected $dates = [
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    protected $fillable = [
        'updated_at',
        'created_at',
        'deleted_at',
        'category_id',
        'author_name',
        'text_answer',
        'author_email_id',
        'ask_question_id',
    ];

    public static function boot()
    {
        parent::boot();

        Respone::Observe(new \App\Observers\ResponeObserver );

    }

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id');
    }

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

    public function ask_question()
    {
        return $this->belongsTo(AskQuestion::class, 'ask_question_id');
    }
}

AskQuestion.php

protected $dates = [
    'created_at',
    'updated_at',
    'deleted_at',
];

protected $fillable = [
    'email',
    'created_at',
    'updated_at',
    'deleted_at',
    'text_question',
    'objet_question',
    'assigned_to_user_id',
];

public static function boot()
{
    parent::boot();

    AskQuestion::Observe(new \App\Observers\AskQuestionObserver );

    static::addGlobalScope(new CollaborateurScope);
}

/**
* In this method may be it should belongsto instead of hasmany
*/
public function respones()
{
    return $this->hasOne(Respone::class, 'ask_question_id', 'id');
}

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

User.php

public function askQuestionRespones()
{
    return $this->hasMany(Respone::class, 'user_id', 'id');
}

show.blade.php

               <div class="card-body">
                <table class="table table-bordered table-striped">
                    <tbody>
                        <tr>
                            <th>
                                {{ trans('Thématique') }}
                            </th>
                            <td>
                                {{ $respone->category->name }}
                            </td>
                        </tr>
                        <tr>
                            <th>
                                {{ trans('Nom auteur') }}
                            </th>
                            <td>
                                {{ $respone->author_name }}
                            </td>
                        </tr>
                        <tr>
                            <th>
                                {{ trans('Auteur email') }}
                            </th>
                            <td>
                                {{ $respone->author_email->email }}
                            </td>
                        </tr>
                        <tr>
                            <th>
                                {{ trans('Question') }}
                            </th>
                            <td>
                                {{ $respone->ask_question->text_question }}
                            </td>
                        </tr>
                        <tr>
                            <th>
                                {{ trans('Réponse') }}
                            </th>
                            <td>
                                {{ $respone->text_answer }}
                            </td>
                        </tr>
                    </tbody>
                </table>

ResponesController.php for the admin part

public function show(Respone $respone)
{
    abort_if(Gate::denies('respone_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');

    $respone->load('category', 'author_email', 'ask_question');

    return view('admin.respones.show', compact('respone'));
}

ResponeController.php for the front part

public function show(Respone $respone, Category $category,
                     User $user, AskQuestion $ask_question)
{
    $respone->load('author_email', 'category', 'ask_question');

    $ask_question->load('Respones');

    return view('respones.show', compact('respone', 'ask_question'));
}
2
Could you add your controller ?Foued MOUSSI

2 Answers

1
votes

one of these relations is returning null

$respone->category
$respone->ask_question
$respone->author_email

so when you try to access a property on the empty relation (on the null value null->attribute), you receive this error.

you can try check the value of each relation before echoing the the attribute, with @if or @isset

@if($respone->category)

@endif

or wrap the relations with the optional helper

<th>
    {{ trans('Thématique') }}
</th>
<td>
    {{ optional($respone->category)->name }}
</td>
0
votes

You may use the null coalescing operator ?? ( introduced in PHP 7). It's used to check if the value is set or null, or in other words, if the value exists and not null, then it returns the first operand, otherwise, it returns the second operand."

<td>
  {{ $respone->category->name ?? 'Default' }} 
</td>