CompanyController.php第126行中的ErrorException:尝试获取非对象的属性

时间:2017-11-20 06:15:03

标签: php mysql laravel controller

我在控制器上收到此错误

我的代码是这样的:

118 public function show($id)
119 {
120        $company_master = DB::table('tbl_company_master')->where('id', $id)->whereNull('tbl_company_master.deleted_at')->first();
121        $company_country = DB::table('tbl_countries')->select('*')->where('id',$company_master->country_id)->first();
122        $countries = DB::table('tbl_countries')->select('*')->orderBy('country_name', 'ASC')->whereNull('deleted_at')->where('status','1')->get();
123        $allstates = DB::table('tbl_states')->select('*')->orderBy('state_name', 'ASC')->where('country_id',$company_master->country_id)->whereNull('deleted_at')->
124                     where('status','1')->get();

125        $company_category = DB::table('tbl_company_industry_type')->where('industry_type_name',$company_master->company_category)->first();
126        $lawids = explode(",", $company_category->lm_id);
127        //dd($lawids);
128        $laws = DB::table('tbl_law_master')->whereNull('deleted_at')
129            ->where('status',1)
130            ->whereIn('id',$lawids)
131            ->get();
132        //dd($laws);

        $companyadmin = DB::table('users')->where('company_id', $id)->where('role', 6)->where('location_id', 0)->whereNull('deleted_at')->get();

        $company_master_class = "active open selected";
        return View::make('admin.companymaster.show')
            ->with('company_category', $company_category)
            ->with('company_master_class', $company_master_class)
            ->with('company_master', $company_master)
            ->with('allstates',$allstates)
            ->with('countries',$countries)
            ->with('company_country',$company_country)
            ->with('laws', $laws)->with('companyadmin', $companyadmin)
            ->with('get_company_details', $get_company_details);

         }

在第126行,我收到错误 尝试获取非对象的属性

这是什么原因。?

2 个答案:

答案 0 :(得分:1)

请更新您的代码,如:

       $company_category = DB::table('tbl_company_industry_type')->where('industry_type_name',$company_master->company_category)->first();
        if(isset($company_category) && !empty($company_category)){
            $lawids = explode(",", $company_category->lm_id);
            //dd($lawids);
            $laws = DB::table('tbl_law_master')->whereNull('deleted_at')
                ->where('status',1)
                ->whereIn('id',$lawids)
                ->get();
            //dd($laws);
        } else {
            $laws = '';
        }

答案 1 :(得分:0)

可能原因是第125行的代码无法找到任何匹配的行。检查是否找到任何 $ company_category