0

I have a model :

class Group extends Property {

    use HasFactory;
    protected $appends = ['logo', 'favicon'];
    protected $hidden = ['image', 'faviconImage'];
    public function getLogoAttribute()
    {
        return $this->image->url ?? null;
    }

    public function getFaviconAttribute()
    {
        return $this->faviconImage->url ?? null;
    }

    public function image()
    {
        return $this->hasOne(GroupImage::class, 'group_id');
    }

    public function faviconImage()
    {
        return $this->hasOne(GroupImage::class, 'group_id');
    }

GroupImage structure

enter image description here

When I tried to call logo in my ressource : $this->logo I get null, even if I have data in my group_images table. I should somehow get data based on localKey ?

GPiter
  • 779
  • 1
  • 11
  • 24
  • `->hasOne(Related::class, 'foreign_key', 'local_key');` is `logo` in your declaration the local key (usually `id`) present in the table `groups` ? Or are you trying to do something else ? maybe add your table**S** structure to your question. – N69S Mar 10 '22 at 12:18
  • Does this answer your question? [What is the difference between BelongsTo And HasOne in Laravel](https://stackoverflow.com/questions/37582848/what-is-the-difference-between-belongsto-and-hasone-in-laravel) – gguney Mar 10 '22 at 12:19
  • @N69S I added the table structure of `GroupImages` but on `Group` I don't have the the logo_id or favicon_id – GPiter Mar 10 '22 at 12:56
  • @N69S is possible to do something like that ? – GPiter Mar 10 '22 at 13:11
  • @klsdskldsd i explicitly made the S in tables bold and capital 7 hours ago. what field are you trying to retrieve and depending on what value/fields ? – N69S Mar 10 '22 at 19:22

0 Answers0