Laravel : Trying to get property of non-object

11,095

Solution 1

your query returning array. If you dump it out, you might find that it's an array and all you need is an array access ([]) instead of an object access (->).

Solution 2

Try calling it like this:

$article->question['title']

Hope that helps!

Share:
11,095
Salem loress
Author by

Salem loress

Updated on June 04, 2022

Comments

  • Salem loress
    Salem loress almost 2 years

    I have problem with laravel , in my localhost everthing is fine but on my shared hosting i got problem even is the same php version

    For example : I want to get question title using $answer->question->title but i got error Trying to get property of non-object (View: /new/resources/views/users/show.blade.php) But the project work fine in my localhost

    Controller :

    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'));
        }
    

    View blade :

     <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>
    

    In my localhost is working fine but in shared hosting i got :

    ErrorException (E_ERROR) Trying to get property of non-object (View: /new/resources/views/users/show.blade.php) Previous exceptions Trying to get property of non-object (0)

    Please could you help me to fix that ?

    • dazed-and-confused
      dazed-and-confused over 4 years
      Did you check to make sure all of your Models are uploaded as well?
    • Salem loress
      Salem loress over 4 years
      Yes i did and i checked other pages everthing work only this page and i cleaned the cache
    • Salem loress
      Salem loress over 4 years
      I tried {{$t->question}} i got array included the title id ... but when i tried {{$t->question->title}} i got the first error
  • Salem loress
    Salem loress over 4 years
    Can you give me an example please ?
  • Bilawal Awan
    Bilawal Awan over 4 years
    $article->question['id'] like this
  • Admin
    Admin over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.