sql - Integrity constraint violation: 1452 fillable also not worked
问题描述
i have one pivot table category_product where i want to store product_id category_id
It works correctly from phpmyadmin but in laravel not worked
i tried fillable but still not work same error
PDOException::("SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child
row: a foreign key constraint fails (new
.category_product
, CONSTRAINT category_product_category_id_foreign
FOREIGN KEY (category_id
) REFERENCES categories
(id
) ON DELETE CASCADE)")
Category.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['parent_id'];
public function childs() {
return $this->hasMany('App\Category', 'parent_id');
}
public function products()
{
return $this->belongsToMany('App\Product');
}
}
Product.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function presentPrice() {
return "Rs ".number_format($this->price);
}
public function categories() {
return $this->belongsToMany('App\Category');
}
}
CategoryProduct.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class CategoryProduct extends Model
{
protected $table = 'category_product';
protected $fillable = ['product_id', 'category_id'];
}
2018_08_15_224031_create_category_product_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoryProductTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('category_product', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned()->nullable();
$table->foreign('product_id')->references('id')
->on('products')->onDelete('cascade');
$table->integer('category_id')->unsigned()->nullable();
$table->foreign('category_id')->references('id')
->on('categories')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('category_product');
}
}
ProductTableSeeder.php
<?php
use App\Product;
use Illuminate\Database\Seeder;
class ProductsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// Laptops
for ($i=1; $i <= 7; $i++) {
Product::create([
'name' => 'Laptop '.$i,
'slug' => 'laptop-'.$i,
'details' => [13,14,15][array_rand([13,14,15])] . ' inch, ' . [1, 2, 3][array_rand([1, 2, 3])] .' TB SSD, 32GB RAM',
'price' => rand(30000, 60000),
'description' =>'Lorem '. $i . ' ipsum dolor sit amet, consectetur adipisicing elit. Ipsum temporibus iusto ipsa, asperiores voluptas unde aspernatur praesentium in? Aliquam, dolore!',
'image' => 'products/dummy/laptop-'.$i.'.jpg',
'images' => '["products\/dummy\/laptop-2.jpg","products\/dummy\/laptop-3.jpg","products\/dummy\/laptop-4.jpg"]',
])->categories()->attach(4);
}
// Phones
for ($i = 1; $i <= 7; $i++) {
Product::create([
'name' => 'Phone ' . $i,
'slug' => 'phone-' . $i,
'details' => [16, 32, 64][array_rand([16, 32, 64])] . 'GB, 5.' . [7, 8, 9][array_rand([7, 8, 9])] . ' inch screen, 4GHz Quad Core',
'price' => rand(20000, 50000),
'description' => 'Lorem ' . $i . ' ipsum dolor sit amet, consectetur adipisicing elit. Ipsum temporibus iusto ipsa, asperiores voluptas unde aspernatur praesentium in? Aliquam, dolore!',
'image' => 'products/dummy/phone-'.$i.'.jpg',
'images' => '["products\/dummy\/phone-2.jpg","products\/dummy\/phone-3.jpg","products\/dummy\/phone-4.jpg"]',
])->categories()->attach(2);
}
// Cameras
for ($i = 1; $i <= 11; $i++) {
Product::create([
'name' => 'Camera ' . $i,
'slug' => 'camera-' . $i,
'details' => 'Full Frame DSLR, with 18-55mm kit lens.',
'price' => rand(40000, 150000),
'description' => 'Lorem ' . $i . ' ipsum dolor sit amet, consectetur adipisicing elit. Ipsum temporibus iusto ipsa, asperiores voluptas unde aspernatur praesentium in? Aliquam, dolore!',
'image' => 'products/dummy/camera-'.$i.'.jpg',
'images' => '["products\/dummy\/camera-3.jpg","products\/dummy\/camera-4.jpg","products\/dummy\/camera-5.jpg"]',
])->categories()->attach(5);
}
// Select random entries to be featured
Product::whereIn('id', [1, 6, 8, 12, 15, 18, 20, 21, 23,22, 24, 13, 10])->update(['featured' => true]);
}
}
解决方案
You want to sync your products and categories but, mysql can not find given category_id.
Probably you forgot to create those rows on category table.
推荐阅读
- html - 我该怎么做才能使我的代码在 2 个不同的屏幕上看起来不错
- docker - 如何在 Jenkins 脚本化管道中使用 dockerhub-notification-plugin?
- php - 有没有办法在 MySQL 更新查询中使用变量
- javascript - 如何从 JQuery AJAX 生成的 HTML 数据中获取属性 id?JavaScript
- node.js - 如何通过 REST API 从 IONIC typescript 读取 node.js app.js 中的数据?
- go - 调用结构上的方法时,Goroutines 不起作用
- makefile - 由于安装 ROS 时 CPU 使用率达到 100,Raspberry Pi 冻结
- javascript - 如何从javascript中的表单数据中删除选定的输入对象?
- python - 在购物车模型中总计 Django Admin 中每个 TabularInline 条目的小计
- birt - 使用 json 字符串作为报告参数将 JSON 转换为 POJO