Trying to get property of non-object in laravel 5.5

♀尐吖头ヾ 提交于 2019-12-24 02:57:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!