Home > Software design >  How to combine an object with an arrow operator and then a custom variable
How to combine an object with an arrow operator and then a custom variable

Time:05-17

I'm working with Laravel 8 and I need to concatenate an object with an arrow operator and then a custom variable like this:

$fullStr = "product_attr_correction";
dd($product->$fullStr); // return null incorrectly

So basically, at the table products I have a column name product_attr_correction and it already has a value in it.

But the result of dd($product->$fullStr) return null incorrectly.

But when I do dd($product->product_attr_correction), I get the proper result value.

So the question is, how can I combine an object with an arrow operator and then a custom variable properly?

CodePudding user response:

Use this syntax :

$product->{$fullStr}
  • Related