Search code examples
phplaravelclass-visibility

Laravel, can't acces to object data


I have this output at 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

How can I acces to that last line [id] => 1020?

I tried with $mailable->user, $mailable->mailable, but it says:

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

Solution

  • The properties you need are protected, so you can't access them from the outside, and the API does not offer getter methods. Since version 5.4, Laravel have made these properties public.

    I'd recommend either updating to a newer version of Laravel, writing your own custom class that adds an accessor method, or use the pry bar technique to force the protected variables into the public API.