'How to define the relationship on this scenrio?

I have a newsletter table with this structure:

newsletter table:

id, title, title_color_id, background_color_id, description

And this table will have only one record.

I have a Nova resource to allow to create this initial record:

class Newsletter extends Resource{
  public function fields(Request $request)
    {
        return [
            ID::make(__('ID'), 'id')->sortable(),

            BelongsTo::make('Bg Color', 'Color', \App\Nova\Color::class),

            BelongsTo::make('Text Color', 'Color', \App\Nova\Color::class),

            Text::make('Title')->sortable(),

            Text::make('Description')->sortable(),

        ];
    }

}

My doub is how to proprly set the relationships on the Newsletter model, because I have two fields that should have an id of a colors table record, but I cannot of course have 2 method colors on the model with those 2 different ccolumns like below. Do you know how to handle this scenario?

The Newsletter model:

class Newsletter extends Model
{
    use HasFactory;

    protected $table = 'newsletter';

    public function color()
    {
        return $this->belongsTo(Color::class, 'title_color_id');
    }

   public function color()
    {
        return $this->belongsTo(Color::class, 'background_color_id');
    }

 }


Solution 1:[1]

This code will not work, you can not have 2 different methods in same class with same name.You need to rename at least one of them.

Relations should look like:

class Newsletter extends Model
{
    use HasFactory;

    protected $table = 'newsletter';

    public function titleColor()
    {
        return $this->belongsTo(Color::class, 'title_color_id');
    }

   public function backgroundColor()
    {
        return $this->belongsTo(Color::class, 'background_color_id');
    }

 }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sona Khachatryan