Search code examples
phplaravelmodelfacade

How to access properties on a facade in php


When I dump this Customer::where('email', '=', $inputObj['email']-first(); the output has a few properties. Some of which have # and others have a +. For example, one is +exists: true, which I can access by ->exists() and it returns me true. Another is #attributes: array:10 [...] which I can see it is an associative array with values in the dump, but I can't access either like this ->attributes or like this ['attributes']. What do the different symbols mean and how can I access the values in the attributes property?

I really want to learn what is happening at a deep level, so any enlightening comments are appreciated (:


Solution

  • Those symbols are used for visibility, please refer to this:

    https://stackoverflow.com/a/4361582/8637968

    You cannot access "attributes" because it is a private property, remember that var_dump() or dd() in Laravel (which stands for dump and die) are meant to be used for debug purposes, that is why you see private properties, but private properties can only be accessed from within the class, in the case of Laravel, you can access the properties inside the "attributes" array as properties itself, for example: assume that for your Customer model you have a column called "name", that column will be inside the "attributes" array, and you can access it like this:

    $customer = Customer::where('email', '=', 'some_email@mail.com')->first();
    $customer->name; // assuming that the model exists, i.e: if ($customer != null) {}