orlyapps / nova-belongsto-depend

Larave Nova BelongsTo Field with Dependcy
MIT License
182 stars 65 forks source link

How to define selected value #57

Open eman-abdulwahab opened 4 years ago

eman-abdulwahab commented 4 years ago

Hey, Not an issue, but is there a way to predefine a selected id? I want to load all values and have the value for the user be selected by default.

aedart commented 4 years ago

I have tried the following, yet does not appear to work (But it does work on regular BelongsTo field).

NovaBelongsToDepend::make('User', 'user', User::class)
    ->options(UserModel::all())
    ->withMeta([
        'belongsToId' => $request->user()->id,
    ]),

However, found this in another issue, which for some reason works:

$userId = $request->user()->id;

NovaBelongsToDepend::make('User', 'user', User::class)
    ->options(UserModel::all())
    ->withMeta([
        'value' => $userId,
        'valueKey' => $userId,
    ]),