I'm creating an app that has a couple photos on the User model, named photo1
and photo2
. The actual photo files are stored on Amazon S3 and the relative path is stored in the database (example: 10206074968017993/photo1.jpg
). I have an accessor in the User model to retrieve the URL for the photo:
public function getPhoto1Attribute()
{
return 'https://s3-'.env('AMAZON_REGION').'.amazonaws.com/'.env('AMAZON_BUCKET').'/'. $this->photo1;
}
This should simply return a full URL of the photo, which should be something like:
https://s3-us-west-2.amazonaws.com/mybucket/10206074968017993/photo1.jpg
But instead it's throwing an error saying
Undefined property: App\\Models\\User::$photo1
I think it's the 1
in the photo1
that is causing the error because if I rename the field to photo
, the code works great. But because the real app is more complicated than I'm describing here, renaming the fields is not an option. What I can I do?
I've just decided to avoid using any integers in my variable names. Though there may be a way to make it work, I just like to keep everything as standardized as possible and I think it's best simply to avoid integers in variable names.