djay
222
19
Eloquent

Trying to get property of non-object

Posted 1 year ago by djay

Hi

My Controller function is -

 public function rulingParty(){

        $states = State::with('ruling')->get();
        return view('state.ruling', compact('states'));
        //return $states;
        //dd($states);
    }

In the Model State

 public function ruling(){

        // State has one ruling party
        return $this->belongsTo('App\Party','ruling');
    }

In the View

@foreach($states as $state)                        
          {{$state->ruling->name}}
                           
@endforeach 

Where am I doing wrong? when I simply return the $states from controller it clearly shows all the model with relations but in the view when I put

@foreach($states as $state)                        
          {{$state->ruling->name}}
                           
@endforeach 

it gives trying to get the property of non-object

Thanks please someone suggest me

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.