简体   繁体   English

SQLSTATE[HY000]:一般错误:1364 字段“client_id”没有默认值

[英]SQLSTATE[HY000]: General error: 1364 Field 'client_id' doesn't have a default value

I'm trying to relate the post with the client by sending "client_id" through request post then when I create it, it gives this exception: SQLSTATE[HY000]: General error: 1364 Field 'client_id' doesn't have a default value (SQL: insert into posts ( content , updated_at , created_at ) values (sadadad, 2020-09-17 18:26:38, 2020-09-17 18:26:38))我试图通过通过请求帖子发送“client_id”来将帖子与客户端相关联,然后当我创建它时,它给出了这个异常:SQLSTATE[HY000]:一般错误:1364字段'client_id'没有默认值(SQL:插入postscontent , updated_at , created_at )值(sadad,2020-09-17 18:26:38, 2020-09-17 18:26:38))

 <form role="form" action="{{ route('posts.store') }}" method="POST" enctype="multipart/form-data"> @csrf {{ method_field('POST') }} @include('includes.errors') <input type="hidden" name="client_id" value="{{ $client->id }}"> <textarea type="text" class="form-control form-control-md" placeholder="what's in your mind ?" name="content"></textarea> <div style="margin: 10px 0;"> <input type="file" hidden id='upload-photo' name="image"> <label for="upload-photo" class="post-adds" ><i class="far fa-file-image fa-lg"></i></label> <input type="file" hidden id='upload-video' name="video"> <label for="upload-video" class="post-adds" ><i class="far fa-file-video fa-lg"></i></label> <button class="btn btn-sm btn-outline-primary " type="submit" style="display: inline; float:right;">Post</button> </div> </form>

migrations:

Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->text('content')->nullable();
            $table->integer('likes')->default(0);
            $table->integer('shares')->default(0);
            $table->string('image')->nullable();
            $table->string('video')->nullable();
            $table->timestamps();

            $table->integer('client_id')->unsigned();
            $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade')->onUpdate('cascade');
        });

controller:

$request->validate([
            'video'=> 'mimes:mp4| max:20000',
            'image'=>'image | max:2000',
            'content'=>'required',
        ]);

        $request_data = $request->except('video','image');

        if($request->image){
            $img = Image::make($request->image);

            $img->resize(300, null, function ($constraint) {
                $constraint->aspectRatio();
            });

            $img->save(public_path('dist/img/posts/'.$request->image->hashName()),60);

            $request_data['image'] = $request->image->hashName();
        }

        if($request->video){
            $vid = $request->video;

            $vid_name = rand().'.'.$vid->getClientOriginalExtension();

            $vid->move(public_path('dist/vid/posts'),$vid_name);

            $request_data['video'] = $vid_name;
        }

        Post::create($request_data);

        session()->flash('success',__('site.post_created_successfully'));
        return redirect()->route('posts.index');

You need to add client_id in fillable array in your Post model like so您需要像这样在Post模型的fillable数组中添加client_id

protected $fillable = [
    ...

    'client_id',

    ...
];

Another Solution另一种解决方案

You can add in your Post model您可以添加Post模型

protected $guarded = [];

this will make all attributes mass assignable这将使所有属性都可以批量分配

There is a possibility that can be considered, I have experienced it.有一种可能,可以考虑,我经历过。

  1. The client_id field is not a nullable column so it must have an auto-increment function client_id字段不是nullablenullable列,因此它必须具有自动递增功能

  2. Because the client_id field is not a nullable column please specify its contents when entering a new row由于client_id字段不是nullable列,请在输入新行时指定其内容

According to the official laravel documentation (here) , the ->unsigned() function on a migration just adds the UNSIGNED attribute to the column without adding the AUTO INCREMENT attribute.根据laravel 官方文档(此处) ,迁移中的->unsigned()函数仅将 UNSIGNED 属性添加到列中,而未添加 AUTO INCREMENT 属性。

If this is the case, you have 2 options.如果是这种情况,您有 2 个选择。

  1. Added the ->autoIncrement() function to the client_id column for migration->autoIncrement()函数添加到client_id列以进行迁移
  2. Change the column type to ->foreignId('user_id') (which means delete the existing ->unsigned() function)将列类型更改为->foreignId('user_id') (即删除现有的->unsigned()函数)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLSTATE [HY000]:一般错误:1364 字段“agent_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'agent_id' doesn't have a default value SQLSTATE[HY000]:一般错误:1364 字段“parent_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'parent_id' doesn't have a default value SQLSTATE[HY000]:一般错误:1364 字段“country_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'country_id' doesn't have a default value SQLSTATE [HY000]:一般错误:1364 字段“trans_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'trans_id' doesn't have a default value Laravel Voyager SQLSTATE [HY000]:常规错误:1364字段“ id”没有默认值 - Laravel Voyager SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段&#39;author_id&#39;没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'author_id' doesn't have a default value Laravel SQLSTATE [HY000]:常规错误:1364字段“ id”没有默认值 - Laravel SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段“ reservation_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'reservation_id' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段“ mov_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'mov_id' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段“照片”在laravel 5.5中没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'photo' doesn't have a default value in laravel 5.5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM