2

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: "[email protected]"} in the database.

This is how I try do to it:

Text::make('To Name', 'toName')
            ->sortable()
            ->fillUsing(
                function ($request, $model) {
                    return $request->toName;
                }
            ),

Text::make('To Email', 'toEmail')
            ->sortable()
            ->fillUsing(
                function ($request, $model) {
                    return $request->toEmail;
                }
            ),

Text::make('To Url', 'toUrl')
            ->sortable()
            ->fillUsing(
                function ($request, $model) {
                    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?

1

1 Answer 1

0

Check this code, use Laravel mutator and some changes in your fillUsing. Note toName, toEmail, toUrl are virtual attributes, and to_json contain json values of these columns in model!

// database\migrations\2019_08_28_045853_create_infos_table.php
...
        Schema::create('infos', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->string('label',100);
            $table->json('to_json');
        });

// app\Nova\Info.php
...    
public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            Text::make('Label'),
            Text::make('To Name', 'toName')
                ->sortable()
                ->fillUsing(
                    function ($request, $model) {
                        if(!empty($request->toName)){
                            if(!empty($model['to_json'])){
                                $json = json_decode($model['to_json'],true);
                            }else{
                                $json = [];
                            }
                            $json['name'] = $request->toName;
                            $model['to_json'] = json_encode($json);
                        }
                    }
                ),
            Text::make('To Email', 'toEmail')
                ->sortable()
                ->fillUsing(
                    function ($request, $model) {
                        if(!empty($request->toEmail)){
                            if(!empty($model['to_json'])){
                                $json = json_decode($model['to_json'],true);
                            }else{
                                $json = [];
                            }
                            $json['email'] = $request->toEmail;
                            $model['to_json'] = json_encode($json);
                        }
                    }
                ),
            Text::make('To Url', 'toUrl')
                ->sortable()
                ->fillUsing(
                    function ($request, $model) {
                        if(!empty($request->toUrl)){
                            if(!empty($model['to_json'])){
                                $json = json_decode($model['to_json'],true);
                            }else{
                                $json = [];
                            }
                            $json['url'] = $request->toUrl;
                            $model['to_json'] = json_encode($json);
                        }
                    }
                ),
        ];
    }

// app\Info.php
...
class Info extends Model
{
    public function gettoNameAttribute(){
        if(empty($this->attributes['to_json'])){
            return "";
        }
        $json = $this->attributes['to_json'];
        $result = json_decode($json,true);
        if(!empty($result['name'])){
            return $result['name'];
        }else{
            return "";
        }
    }

    public function gettoEmailAttribute(){
        if(empty($this->attributes['to_json'])){
            return "";
        }
        $json = $this->attributes['to_json'];
        $result = json_decode($json,true);
        if(!empty($result['email'])){
            return $result['email'];
        }else{
            return "";
        }
    }

    public function gettoUrlAttribute(){
        if(empty($this->attributes['to_json'])){
            return "";
        }
        $json = $this->attributes['to_json'];
        $result = json_decode($json,true);
        if(!empty($result['url'])){
            return $result['url'];
        }else{
            return "";
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.