Laravel,无法访问对象数据

Laravel, can't acces to object data

我在 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

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

我建议更新到 Laravel 的较新版本,编写您自己的自定义 class 添加访问器方法,或者使用 pry bar technique 强制受保护的变量进入publicAPI。