将默认值传递到数据库

时间:2018-10-15 06:02:07

标签: phpmyadmin laravel-5.4 postman

  

如果用户离开输入字段,我想将默认值传递给数据库   空白   如果用户未选择隐私,即在公共或私人   数据库应该公开发布

public function saveMedia(Request $request){
        $uid = $request->user()->id;
        $fileExtensionValidation = Validator::make($request->all(), [
            'file' => 'mimes:jpeg,png,bmp,tiff'
        ]);
        if(!$fileExtensionValidation->fails()){
            $file = $request->file('file')->store('');
            $filename = str_replace('public/','',$file);
            $id = DB::table('medias')->insertGetId([
                'name'=>$filename,
                'hash'=>hash('sha256', \Storage::get($file)),
                'uploader_id'=>$uid,
                'hide'=> 0,
                'created_at' => Carbon::now()
               ]);
            $url = \Storage::temporaryUrl($filename, now()->addDays(2));

            return ['id'=>$id, 'url'=>$url];
       } else{
           return abort(501, 'File format not supported');
       }
    }
  

路由api.php

Route::group(['middleware'=>['auth:api', \App\Http\Middleware\OnlyRegisteredUsers::class]], function(){
    /**
     * Group for registered users only APIs
     */
    Route::post('savePost','UserController@savePost');
});
  

迁移文件

 public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->longText('content');
            $table->string('short_description');
            $table->unsignedInteger('media_id')->nullable();
            $table->foreign('media_id')->references('id')->on('medias');
            $table->unsignedInteger('creator_id');
            $table->foreign('creator_id')->references('id')->on('users');
            $table->boolean('hide')->default(0);
            $table->timestamps();
        });
    }
  

我在此次迁移中添加了新列

public function up()
    {
      Schema::table('posts', function (Blueprint $table) {
        $table->integer('privacy')->after('creator_id');
        });
    }
  

如果我在Privacy字段中传递一个值将进入DB,但是如果我离开它   空白将给我一个错误。

SQLSTATE [23000]:违反完整性约束:1048列“ privacy”不能为空(SQL:插入poststitlecontentshort_description,{ {1}},media_idcreator_idprivacycreated_at)值(快速阅读,社交媒体应用,您可以上传帖子媒体,可以对帖子发表评论,可以upvote downvote posts,1,25,,2018-10-15 06:00:59,2018-10-15 06:00:59))“,

0 个答案:

没有答案