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

CompanyController.php第126行中的ErrorException:尝试获取非对象的属性,php,mysql,laravel,controller,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_countri

我的控制器上出现此错误

我的代码如下:

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行,我得到一个错误 尝试获取非对象的属性


原因是什么?

可能是因为第125行中的代码找不到任何匹配行。检查是否找到任何$company\u类别

请更新您的代码,如下所示:

       $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 = '';
        }

检查
$company\u category
是否为空。@ErfanAhmedEmon yes-erfan当我执行dd($company\u category)时,它显示为空;但我在这个表中有数据tbl_company_industry_typecheck是否是这个值字符串“$company_category->lm_id”,explode second param应该是字符串,check$company_category返回值也不存在$company\u category->lm\u id打印($company\u category)并查看结果,然后尝试查找
$company\u category
返回空值的原因。检查
$company\u master
是否也有空值。