簡體   English   中英

SQLSTATE[HY000]:一般錯誤:1364 字段“client_id”沒有默認值

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

我試圖通過通過請求帖子發送“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');

您需要像這樣在Post模型的fillable數組中添加client_id

protected $fillable = [
    ...

    'client_id',

    ...
];

另一種解決方案

您可以添加Post模型

protected $guarded = [];

這將使所有屬性都可以批量分配

有一種可能,可以考慮,我經歷過。

  1. client_id字段不是nullablenullable列,因此它必須具有自動遞增功能

  2. 由於client_id字段不是nullable列,請在輸入新行時指定其內容

根據laravel 官方文檔(此處) ,遷移中的->unsigned()函數僅將 UNSIGNED 屬性添加到列中,而未添加 AUTO INCREMENT 屬性。

如果是這種情況,您有 2 個選擇。

  1. ->autoIncrement()函數添加到client_id列以進行遷移
  2. 將列類型更改為->foreignId('user_id') (即刪除現有的->unsigned()函數)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM