Home > Enterprise >  could not access laravel object data
could not access laravel object data

Time:08-03

$cntct = DB::table('products') 
            ->join('merchants','merchants.id','=','products.merchant_id') 
            ->where('products.id',$id)
            ->get();

dd($cntct[0]->address);die;

data show from this query

"{
    "address":"dummy"
    "state":"dummy",
    "zip":"45345",
    "country":"Pakistan",
    "city":"dummy"
}"

I want access this address but when i write dd($cntct[0]->address->address);die; error show

Trying to get property 'address' of non-object

CodePudding user response:

Maybe, change the

->get()

to

->first()

Cuz you only want one value (you give the products id)

And then

dd($cntct->address);  // no need of die

CodePudding user response:

You can try foreach ($cntct as $item){ //your code }. Maybe the query does not return any result and that's the cause of an error - Trying to get property 'address' of non-object.

  • Related