1
votes

Recently I've been trying to create Nova resource which depends on the other resource which provides the information for the main resource.

I have a table contest_entries which has the following fields:

  • id
  • contest_id
  • user_id

with the following relations

    public function contest() : BelongsTo {
        return $this->belongsTo(Contest::class, 'contest_id', 'id');
    }

    public function user() : BelongsTo {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

Also, i have a table contest_submissions with the following fields:

  • id
  • entry_id
  • task_id
  • comment
  • approved
  • declined with the following relations:
public function entry() : BelongsTo {
    return $this->belongsTo(ContestEntry::class, 'entry_id', 'id');
}

public function user() : BelongsTo {
    return $this->entry->user();
}

public function contest() : BelongsTo {
    return $this->entry->contest();
}

public function task() : BelongsTo {
    return $this->belongsTo(Task::class, 'task_id', 'id');
}

I have no problem in fetching this data on the index and details view of Nova, everything 'just works', however, when I try to update the resource, I'm getting the error that user() or contest() is called on null.

I've tried the following,

return [
    BelongsTo::make('Contest', 'contest', Contests::class)->exceptOnForms(),
    BelongsTo::make('Task', 'task', ContestTasks::class)->exceptOnForms(),
    BelongsTo::make('User', 'user', AccountUsers::class)->exceptOnForms(),
]

But for some reason, Nova is still trying to fetch these relationships ever when i explicitly tell it not to.

Any ideas are greatly appreciated, because it works everywhere, except on the update view (create view is explicitly disabled since the submissions are created by the user on the frontend)

1

1 Answers

0
votes

You should also chain a hideWhenUpdating() constraint to it.

return [
    BelongsTo::make('Contest', 'contest', Contests::class)
        ->hideWhenUpdating()
        ->exceptOnForms(),

    BelongsTo::make('Task', 'task', ContestTasks::class)
        ->hideWhenUpdating()
        ->exceptOnForms(),

    BelongsTo::make('User', 'user', AccountUsers::class)
        ->hideWhenUpdating()
        ->exceptOnForms(),
]