mardi 27 août 2019

Laravel Nova ignore a field if empty using ->fillUsing()

I have 3 fields "name", "email" and "url". These 3 fields are casted to a json in 1 column in my database.

Now if you fill in just the url I want to save only {url: "value"} in the database. If you fill in email and name I want to save only {name: "john", email: "john@gmail.com"} in the database.

This is how I try do to it:

Text::make('To Name', 'toName')
            ->sortable()
            ->fillUsing(
                function ($request, $model) {
                    if ($model->typesProvider->provider->handler = WebHookProvider::class) {
                        return;
                    }

                    return $request->toName;
                }
            ),

Text::make('To Email', 'toEmail')
            ->sortable()
            ->fillUsing(
                function ($request, $model) {
                    if ($model->typesProvider->provider->handler = WebHookProvider::class) {
                        return;
                    }

                    return $request->toEmail;
                }
            ),

Text::make('To Url', 'toUrl')
            ->sortable()
            ->fillUsing(
                function ($request, $model) {
                    if ($model->typesProvider->provider->handler != WebHookProvider::class) {
                        return;
                    }

                    return $request->toUrl;
                }
            ),

But I keep getting this error:

General error: 1364 Field 'to' doesn't have a default value

Am I returning something wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire