【问题标题】:SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'cat_id' cannot be nullSQLSTATE [23000]:违反完整性约束:1048 列 'cat_id' 不能为空
【发布时间】:2017-04-15 11:39:57
【问题描述】:

我在向表中输入数据时遇到此错误

class EntryController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */


    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */

    public function entry(Request $request){

        $category = category::create(['cat_name' => $request->input('cat_name')]);

         $product= product::create([
            'Product_name'=>$request->input('Product_name'),
            'Produc_quantity'=>$request->input('Produc_quantity'),
            'Product_Desc' =>$request->input('Product_Desc'), 
            'cat_id'=>$category->cat_id]);


class category extends Model
{
    protected $fillable = array('cat_description','cat_name');


    public function product()
    {
        return $this->hasMany(product::class);


    }
//
}

这些是我的模型

class product extends Model
{
    protected   $fillable = array('Product_name','Product_Desc',
                                  'Produc_quantity','sale_price',
                                  'cost_price');
    public function category()
    {
        return $this->belongsTo(category::class);


    }
        //
}

我已经从产品模型中的 $fillables 中删除了 cat_id,这导致了这个错误,我不明白这一点我检查了很多论坛和问题,但我没有找到答案 这是产品表

 Schema::create('products', function (Blueprint $table) {
        $table->increments('product_id');
        $table->string('Product_name');
        $table->string('Product_Desc');
        $table->integer('sale_price');
        $table->integer('cost_price');
        $table->integer('Produc_quantity');
       $table->integer('cat_id')->unsigned();
        $table->foreign('cat_id')->references('cat_id')->on('categories');
        $table->timestamps();

这是分类表

  Schema::create('categories', function (Blueprint $table) {
        $table->increments('cat_id');
        $table->string('cat_name');
        $table->string('cat_description');


        $table->timestamps();

【问题讨论】:

  • 这可能是一个错字,但你的第一堂课在你开始第二堂课之前没有结束}
  • cat_id 是您数据库中的自增字段吗?
  • 是的,它是自动增量
  • 去掉 $fillable 上的 cat_id,因为它是自动递增的。
  • 我从 $fillables 中删除 cat_id 现在我得到了这个 SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row:

标签: php laravel


【解决方案1】:

您确定该类别已成功创建吗?
如果是这样,也许您需要将cat_id 添加到您的$fillable
此外,当使用 cat_id 之类的自定义 ID 时,您需要添加

public $primaryKey = 'yourkey' 

【讨论】:

  • 是的,cat_id已经创建了,你的意思是我要这样写return $this->belongsTo(category::class,'cat_id','cat_id')
  • 是的,如果你的产品中有名为 cat_id 的 id,你应该把它放在本地密钥中.. 如果它只是 id .. 然后把 foreign_key 作为 cat_key
  • 是的,我在 products 中有 cat_id,但它也不起作用
  • 我的意思是产品的主键,如果它的名字不是id,你需要用你给你的id的名字替换local_key,你能提供两个模型的迁移吗?
  • Schema::create('categories', function (Blueprint $table) { $table->increments('cat_id'); $table->string('cat_name'); $table->string('cat_description'); $table->timestamps();
【解决方案2】:

我解决了这个问题实际上我的product模型$fillables有一个问题我在我的类别模型cat_id添加$fillables这解决了这个问题谢谢你的帮助

【讨论】:

    猜你喜欢
    • 2021-02-01
    • 2019-11-20
    • 2021-08-16
    • 2019-10-06
    • 2020-10-25
    • 2017-02-28
    • 2017-12-26
    • 2019-11-24
    • 2018-01-25
    相关资源
    最近更新 更多