问题
I am trying to show my variables values to view page, but it's showing the error of trying to get property of non-object. Please someone help me. Here is my Controller-
public function myAffiliates(Request $request) {
if ($user_id = Sentinel::getUser()->id) {
$affiliates = DB::table('affiliate_users')
->where('affiliate_users.user_id', $user_id)
->first();
}
//dd($affiliates);
return view('dashboard.affiliates.show',['affiliates' => $affiliates]);
}
And I wrote to my view page like this-
@foreach ($affiliates as $affiliate)
<ul>
<li>First Upline - {{ $affiliate->first_upline_id }} Pts,</li>
<li>Second Upline - {{ $affiliate->second_upline_id }} Pts,</li>
<li>Third Upline - {{ $affiliate->third_upline_id }} Pts.</li>
</ul>
@endforeach
回答1:
If you want to get single record.. use first() in controller and in your view, you don't need to use foreach. But if you want to get more than one record.. use get() in controller and in your view, you have to use foreach.
回答2:
You are trying to iterate on object's properties try this
<ul>
<li>First Upline - {{ $affiliates->first_upline_id }} Pts,</li>
<li>Second Upline - {{ $affiliates->second_upline_id }} Pts,</li>
<li>Third Upline - {{ $affiliates->third_upline_id }} Pts.</li>
</ul>
来源:https://stackoverflow.com/questions/46351893/trying-to-get-property-of-non-object-in-laravel-5-5