Laravel:尝试获取非对象的属性

Sal*_*ess 2 php get laravel

我的 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'));
    }
Run Code Online (Sandbox Code Playgroud)

查看刀片:

 <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>
Run Code Online (Sandbox Code Playgroud)

在我的本地主机中工作正常,但在共享托管中我得到:

ErrorException (E_ERROR) 尝试获取非对象的属性(查看:/new/resources/views/users/show.blade.php) 先前的异常 尝试获取非对象的属性 (0)

请你帮我解决这个问题吗?

小智 5

尝试这样称呼它:

$article->question['title']
Run Code Online (Sandbox Code Playgroud)

希望有帮助!