У меня есть 4 таблицы (пользователи, ask_questions, ответы, категории). Я пытаюсь показать каждый ответ на поставленный вопрос, но я получаю эту ошибку: Пытаюсь получить свойство 'name' не-объекта (View: C: \ xampp \ htdocs \ COMPLIANCE-FAQ \ resources \ views \ responsees \ show.blade. php)
респон модели. 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');
}
Пользователь. 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 для административной части
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 для передней части
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'));
}