laravel - php eval() get attribute of object
Solution:
$str = 'Post name is ' . $post->name;
echo $str;
I don't recommend using eval()...
Answer
Solution:
This happens because $post will be evaluated directly in the eval function. Thus outputing {"id":1,"name": "New feature"}. Using eval is not recommended and certainly not for outputting a simple string. You can go with this instead:
$post = Post::find($id);
$str = "Post name is {$post->name}";
echo $str;
If you really need eval for some reason (there is probably a better way than eval), then this should do it:
$post = Post::find($id);
$name = $post->name;
$str = 'Post name is'.$name;
eval("\$str = \"$str\";");
Source