试图在laravel 5.5中获取非对象的属性 [英] Trying to get property of non-object in laravel 5.5

查看:57
本文介绍了试图在laravel 5.5中获取非对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示变量值以查看页面,但是它显示试图获取非对象属性的错误.请有人帮我. 这是我的控制器-

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

推荐答案

如果要获取单个记录,请在控制器中和视图中使用first(),而无需使用foreach. 但是,如果要获取多个记录,请在控制器和视图中使用get(),则必须使用foreach.

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.

这篇关于试图在laravel 5.5中获取非对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆