简体   繁体   中英

Access $model in the fields function (laravel nova)

public function fields(NovaRequest $request)
{
    if (IBAN::find($model->id_user)->iban ?? false) {
     return [
          // Exemple
     ];
    }
    else {
     return [
          Text::make("IBAN:", 'iban')->required()->rules('required')->placeholder("PT000000"),
     ];
    }
}

I need to access $models in the fields function to get the id of the selected user.

I tried to use:

$request->user()->id

Instead of using:

$model->user_id

When I use $request->user()->id it returns my account id instead of returning the id of the selected user.

But if I do $model->user_id in the handle function it returns the id of the selected user correctly.

How can I access the $model in the fields function?

Laravel Nova resource injects the Model to each resource object So in the table, every row is an object from the resource class so directly you can use recourse object like below

public function fields(NovaRequest $request)
{
    if (IBAN::find($**this**->id_user)->iban ?? false) {
     return [
          // Exemple
     ];
    }
    else {
     return [
          Text::make("IBAN:", 'iban')->required()->rules('required')->placeholder("PT000000"),
     ];
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM