首页 > 解决方案 > 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)

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

标签: phplaravelget

解决方案


尝试这样称呼它:

$article->question['title']

希望有帮助!


推荐阅读