I have a model called Book for a table called books, that has a field called cover_image.
However, I want to define an accessor for the field cover_image instead of just retrieving the default value.
This is how I attempted to do this:
class Book extends Model {
public function getCoverImageAttribute() {
if ($this->cover_image === null) { // Not sure how to check current value?
return "a.jpg"
}
return $this->cover_image;
}
}
However the above of course does not work for two reasons:
- Calling
$this->cover_imageagain would cause recursion - Laravel dosen't like that I created an accessor for an existing field in the database
How can I fix this?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire