Home > Enterprise >  set input to empty string or null if it is not filled and save other datas in vue with laravel
set input to empty string or null if it is not filled and save other datas in vue with laravel

Time:02-22

I have about seven input field in my newinsight i want some of the input field to be optional either it is field or not the post should be saved to database, i get error 500 if i leave some inputs empty. I have tried an it seems not working can anyone help out. below is my code.

my controller

public function store(Request $request)
{
    $insight= new Insight();
    $insight->slug = $request->slug;
    $insight->body = $request->body;
    $insight->head1 = $request->head1;
    $insight->paragraph1 = $request->paragraph1;
    $insight->head2 = $request->head2;
    $insight->paragraph2 = $request->paragraph2;
    $insight->head3 = $request->head3;
    $insight->paragraph3 = $request->paragraph3;
    $insight->head4 = $request->head4;
    $insight->paragraph4 = $request->paragraph4;
    $insight->head5 = $request->head5;
    $insight->paragraph5 = $request->paragraph5;
    $insight->head6 = $request->head6;
    $insight->paragraph6 = $request->paragraph6;
    $insight->head7 = $request->head7;
    $insight->paragraph7 = $request->paragraph7;
    if($request->hasFile('image')){
        $file = $request->file('image');
       $file_name = time(). '.' . $file->getClientOriginalName();
       $file->move(public_path('img/'),$file_name);
       $insight->image = 'img/' . $file_name;
       
    }
  
    $insight->save();
    return response()->json(['message'=>'Saved Successfully'],200); 
}

form

 data() {
        return {
            form: new Form({
             image:null,
             slug:'',
             body: '',
             head1:'',
             paragraph1: '',
             head2:'',
             paragraph2: '',
             head3:'',
             paragraph3: '',
             head4:'',
             paragraph4: '',
             head5:'',
             paragraph5: '',
             head6:'',
             paragraph6: '',
             head7:'',
             paragraph7: '',
            }),
           show: false
        }
    },
    methods: {
        onChange(e){
           const file = e.target.files[0]
           this.form.image = file
            console.log("selected file", file) 
        },
        submit(){
            this.form.post('/api/insight', {
                transfromRequest: [function(data, headers){
                    return objectToFormData(data)
                }],
                onUploadProgress: e =>{
                    console.log(e,)
                }
            }).then(({data})=>{
                console.log(data)
            })
            console.log(this.form)
             this.form = ''
             this.form.image = ''
        },

above is my vue js form

CodePudding user response:

Make fields nullable in database. Then fields will be NULL if you doesn't set them.

Migration:

public function up()
{
    Schema::create('insights', function (Blueprint $table) {
        $table->id();
        $table->string('body')->nullable();
        $table->string('head1')->nullable();
        $table->string('paragraph1')->nullable();
        $table->string('head2')->nullable();
        //...etc
        $table->timestamps();
    });
}

And you can do it without errors:

public function store() {
    $insight = new Insight();
    $insight->save();
}

DB result:

mysql> select * from insights;
 ---- ------ ------- ------------ ------- --------------------- --------------------- 
| id | body | head1 | paragraph1 | head2 | created_at          | updated_at          |
 ---- ------ ------- ------------ ------- --------------------- --------------------- 
|  1 | NULL | NULL  | NULL       | NULL  | 2022-02-22 07:38:58 | 2022-02-22 07:38:58 |
 ---- ------ ------- ------------ ------- --------------------- --------------------- 
1 row in set (0.00 sec)

CodePudding user response:

Try this:

public function store(Request $request)
{

    // validate your compulsory field here.
    $insight= new Insight();

    foreach($request->all() as $key => $value){
      $insight[$key] = $value;
    }

    unset($insight[_token]); // if that's your csrf name

    if($request->hasFile('image')){
        $file = $request->file('image');
       $file_name = time(). '.' . $file->getClientOriginalName();
       $file->move(public_path('img/'),$file_name);
       $insight[image] = 'img/' . $file_name;
       
    }
  
    $insight->save();
    return response()->json(['message'=>'Saved Successfully'],200); 
}

  • Related