I have an application allows creating a task. A task belongs to a campaign.
I have a creating button in campaign detail page, if user click that button, then the URL will be like this: http://demo.test/nova/resources/tasks/new?viaResource=campaigns&viaResourceId=1&viaRelationship=
.
As you can see, I added the resource id
, so I can use them when creating a task. Something like that:
$campaign = Campaign::find($request->viaResourceId);
...
Select::make('Campaign', 'campaign_id')
->options([$campaign->id => $campaign->name])
->displayUsingLabels()
->withMeta(['value' => $campaign->id])
->hideWhenUpdating()
->readonly(true),
That code works unless I use a BelongsTo
field for another relationship (in this case is task group). It seems the BelongsTo
field made another request, and it didn't attach the viaResourceId
in the URL so I coud not access my campaign
variable. For example, $campaign->id
-> I got the error: Trying to get property 'id' of non-object
Change app\\Nova\\Task.php in fields part, to this:
public function fields(Request $request)
{
if($request->editMode=="create" && !empty($request->viaResource) && !empty($request->viaResourceId)){
$campaign = Campaign::find($request->viaResourceId);
return [
ID::make()->sortable(),
Text::make('Name')->sortable(),
Select::make('Campaign', 'campaign_id')
->options([$campaign->id => $campaign->name])
->displayUsingLabels()
->withMeta(['value' => $campaign->id])
->hideWhenUpdating()
->readonly(true),
BelongsTo::make('Group')->display('name'),
];
}
return [
ID::make()->sortable(),
Text::make('Name')->sortable(),
BelongsTo::make('Campaign')->display('name'),
BelongsTo::make('Group')->display('name'),
];
}
I checked it in my nova draft project, it runs well, not any error!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.