SQLSTATE[22007]:無效的日期時間格式:1366 不正確的整數值:列的“耳機”
inventory。products.category_id在第 1 行。
我的產品名稱是字串型別,它與類別表連接。但是類別 id 是整數型別。
查詢代碼:
public function index()
{
//
$products = DB::table('products')
->join('categories','products.category_id','categories.id')
->join('suppliers','products.supplier_id','suppliers.id')
->select('categories.category_name','suppliers.name','products.*')
->orderBy('products.id','DESC')->get();
;
return response()->json($products);
}
這是我的產品表
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->integer('category_id');
$table->string('product_name');
$table->string('product_code');
$table->string('root')->nullable();
$table->string('buying_price')->nullable();
$table->string('selling_price');
$table->integer('supplier_id');
$table->string('buying_date');
$table->string('image')->nullable();
$table->string('product_quantity');
$table->timestamps();
});
這是類別表:
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('category_name');
$table->timestamps();
});
}
請修復我哪里做錯了?
uj5u.com熱心網友回復:
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->bigInteger('category_id')->unsigned();
$table->string('product_name');
$table->string('product_code');
$table->string('root')->nullable();
$table->string('buying_price')->nullable();
$table->string('selling_price');
$table->integer('supplier_id');
$table->string('buying_date');
$table->string('image')->nullable();
$table->string('product_quantity');
$table->timestamps();
});
$table->foreign("category_id")->references("id")->on("categories");
////////////////// end migration ////////////////////////////
$products = DB::table('products')
->join('categories','products.category_id','=','categories.id')
->join('suppliers','products.supplier_id','=','suppliers.id')
->select('categories.category_name','suppliers.name','products.*')
->orderBy('products.id','=','DESC')->get();
你應該像這樣編輯代碼。在我希望它是作業之后。
uj5u.com熱心網友回復:
在遷移中使 id 列自動遞增,如下所示
$table->bigIncrements('id');
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/331492.html
