我对laravel有问题,在我的本地主机中一切都很好,但是在我的共享主机上,即使是相同的php版本,我也遇到了问题
例如:我想使用$ answer-> question-> title来获取问题标题,但是我在尝试获取非对象的属性时出错(查看:/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)试图获取非对象的属性(查看:/new/resources/views/users/show.blade.php)以前的异常试图获取非对象的属性(0)
请您能帮我解决这个问题吗?
您的查询返回数组。如果将其转储出去,您可能会发现它是一个数组,而您所需要的只是数组访问([])
而不是对象访问(->).
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句