php - Laravel:试图获取非对象的属性
问题描述
我对 laravel 有问题,在我的本地主机上一切都很好,但在我的共享主机上我遇到了问题,即使是相同的 php 版本
例如:我想使用 $answer->question->title 获取问题标题,但出现错误 Trying to get property of non-object (View: /new/resources/views/users/show.blade.php) 但是项目在我的本地主机中工作正常
控制器 :
public function show($id)
{
$user = User::find($id);
$answers = \App\Answer::where('user_id','=',$user->id)
->with(['survey'])
->get();
$survey = \App\Survey::pluck('title', 'id')->toArray();
$question = \App\Question::pluck('title', 'id')->toArray();
return view('users.show', compact('user','survey','answers','question'));
}
查看刀片:
<table class="table">
<thead class="thead-light">
<tr>
<th>{{ __('Question') }}</th>
<th>{{ __('Answers') }}</th>
<th>{{ __('Creation Date') }}</th>
</tr>
</thead>
<tbody>
@foreach($answers as $t)
<tr>
<td> {{ $t->question->id }} </td>
<td> {{ $t->answer }} </td>
<td> {{$t->created_at}} </td>
</tr>
@endforeach
</tbody>
</table>
在我的本地主机中工作正常,但在共享主机中我得到:
ErrorException (E_ERROR) Trying to get property of non-object (View: /new/resources/views/users/show.blade.php) 以前的异常 Trying to get property of non-object (0)
请你能帮我解决这个问题吗?
解决方案
尝试这样称呼它:
$article->question['title']
希望有帮助!
推荐阅读
- laravel - 在 url 参数绑定旁边设置 laravel 中路由的默认值
- python - django request.session.clear() 和request.session.flush() 的区别?
- google-cloud-platform - 什么 gcloud 命令会列出具有特定网络标签的所有资源?
- security - 使用 gitlfs 存储库的包含模式给出:批处理响应:找不到存储库或对象:
- java - java中等效的AWS cloudformation的json“Ref”是什么
- csv - 将复杂文本从 RTF 或 PDF 文件转换为 CSV
- angular - 无法在 Angular 中提交表单值
- javascript - 想要 D3 条形图中的重复键
- c++ - Boost.Asio 的底层功能
- python - 有没有办法根据文件名列表在 GCS 存储桶中自动创建文件夹?