【问题标题】:SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: problemSQLSTATE [22007]:无效的日期时间格式:1366 不正确的整数值:问题
【发布时间】:2021-08-22 13:09:24
【问题描述】:
    public function store(Request $request)
{
    $this->validate($request,[
        'title' => 'string|required',
        'summary' => 'string|nullable',
        'is_parent' => 'sometimes|in:1',
        'parent_id' => 'nullable',
        'status' => 'nullable|in:active,inactive'
    ]);
    $data=$request->all();
    $slug=Str::slug($request->title);
    $count=Category::where('slug',$slug)->count();
    if($count>0){
        $slug=$slug.'-'.date('ymdis').'-'.rand(0,999);
    }
    $data['slug']=$slug;
    // return $slug;
    $status=Category::create($data);
    if($status){
        request()->session()->flash('success','Kategori başarıyla eklendi');
    }
    else{
        request()->session()->flash('error','Kategori eklenirken hata oluştu');
    }
    return redirect()->route('category.index');

}

SQLSTATE[22007]:无效的日期时间格式:1366 不正确的整数值:列 kintshop.categories.parent_id 在第 1 行的“活动”(SQL:插入到 categories (@987654326 @, summary, is_parent, parent_id, photo, status, slug, updated_at, created_at) 值 (dsadasd,

dsadasdas

, 1, active , /storage/photos/1/category1.png, 活动, dsadasd, 2021-06-04 13:09:04, 2021-06-04 13:09:04))

这是我的架构

    **Schema::create('categories', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->string('slug')->unique();
        $table->string('photo')->nullable();
        $table->boolean('is_parent')->default(true);
        $table->unsignedBigInteger('parent_id')->nullable();
        $table->enum('status',['active','inactive'])->default('active');
        $table->foreign('parent_id')->references('id')->on('categories')->onDelete('SET NULL');
        $table->mediumText('summary')->nullable();
        $table->timestamps();
    });**

【问题讨论】:

  • dd($data) 并显示您得到的结果并发布迁移文件或表结构

标签: php sql laravel laravel-8


【解决方案1】:

错误很明显。Request 包含 parent_idactivemigration 文件说 parent_idforeign key 您必须传递准确的值才能创建方法。

parent_id 数据类型为Big Integer

查看您的$request->all() 数据

 [▼ "_token" => "LrDekkz85cys4ZFx4vCFmy0VPHwFCh1u7aPIcEqq"

 "title" => "dsadasd" 
 "summary" => "<p>dsadasdas</p>" 
 "files" => null
 "is_parent" => "1" 
 "parent_id" => "active" 
 "photo" => "/storage/photos/1/category1.png"
 "status" => "active" ]

所以更好地更新parent_id 的验证规则只包含整数

【讨论】:

    【解决方案2】:

    表列“parent_id”是 bigInteger,但您的请求中有“活动”(字符串),因此,更改数据库上的数据类型或更新验证规则。

    提示:不要使用 '$request->all()',不安全.. 你可以使用 '$request->only('title', 'summary', 'status' .... .)'。这是从请求中检索参数的安全做法

    【讨论】:

      【解决方案3】:
      $this->validate($request,[
              'title' => 'string|required',
              'summary' => 'string|nullable',
              'is_parent' => 'sometimes|in:1',
              'parent_id' => 'nullable',
              'status' => 'nullable|in:active,inactive'
      ]);
      

      这是错误的...$this->validate([]) 首先接受规则作为参数,而不是请求。也许你必须这样做

      $request->validate([
              'title' => 'string|required',
              'summary' => 'string|nullable',
              'is_parent' => 'sometimes|in:1',
              'parent_id' => 'nullable',
              'status' => 'nullable|in:active,inactive'
      ]);
      

      $validation = Validator::make($request->all(),[rules]);
      

      【讨论】:

        猜你喜欢
        • 2021-04-21
        • 2021-02-14
        • 2021-07-23
        • 1970-01-01
        • 1970-01-01
        • 2021-05-25
        • 2017-11-04
        • 2017-12-10
        • 1970-01-01
        相关资源
        最近更新 更多