Laravel,无法访问对象数据

时间:2019-07-23 15:58:20

标签: php laravel class-visibility

我在print_r($mailable)上有此输出:

Illuminate\Mail\SendQueuedMailable Object
(
    [mailable:protected] => App\Mail\Expired Object
        (
            [user:protected] => App\User Object
                (
                    [fillable:protected] => Array
                        (
                            [0] => name
                            [1] => email
                            [2] => password
                            [3] => demo
                            [4] => email_demo
                        )

                    [hidden:protected] => Array
                        (
                            [0] => password
                            [1] => remember_token
                        )

                    [connection:protected] => 
                    [table:protected] => 
                    [primaryKey:protected] => id
                    [keyType:protected] => int
                    [perPage:protected] => 15
                    [incrementing] => 1
                    [timestamps] => 1
                    [attributes:protected] => Array
                        (
                            [id] => 1020

如何访问最后一行[id] => 1020

我尝试使用$ mailable-> user和$ mailable-> mailable,但提示:

Undefined property: Illuminate\Mail\SendQueuedMailable::$user

1 个答案:

答案 0 :(得分:0)

您需要的属性是protected,因此您不能从外部访问它们,并且the API不提供getter方法。从5.4版开始,Laravel已设置了这些属性public

我建议要么更新到Laravel的较新版本,编写自己的添加访问器方法的自定义类,要么使用pry bar technique将受保护的变量强制进入公共API。