doujizhong8352 2017-11-06 00:24
浏览 39
已采纳

如何在laravel中的另一个表中获取非整数列

I'm using laravel 5.5 and I have 2 columns coupons and carts in my carts table migrations I have coupon_code which will get code from coupons table and the result will be cutoff price from total price in cart.

This is my cart migration:

public function up()
    {
        Schema::create('carts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->integer('product_id')->unsigned();
            $table->integer('coupon_code')->unsigned();
            $table->timestamps();
        });

        Schema::table('carts', function($table) {
            $table->foreign('user_id')->references('id')->on('users');
            $table->foreign('product_id')->references('id')->on('products');
            $table->foreign('coupon_code')->references('code')->on('coupons');
        });
    }

and I get this error when I try to run php artisan migrate

[Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `carts` add constraint `carts_coupon_code_foreign` foreign key (`coupon_co
  de`) references `coupons` (`code`))

  [PDOException]
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

How can I solve that?

Update

Coupon migration

public function up()
    {
        Schema::create('coupons', function (Blueprint $table) {
            $table->increments('id');
            $table->string('code')->unique();
            $table->decimal('amount');
            $table->date('valid_from');
            $table->date('valid_to');
            $table->timestamps();
        });
    }

Idea!

What if I make coupon_code string and nullable in my carts table and then make it scope to validate coupon code from coupons table then cutoff the price? do you think that will work? and if yes, is there someone to help me with that scope?

  • 写回答

1条回答 默认 最新

  • drpp5680 2017-11-06 00:43
    关注

    I noticed that both fields have different datatype, for example in your carts table coupon_code field is an unsigned integer but on coupons table you have code field as strings.

    carts migration:

    public function up()
    {
        Schema::create('carts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->integer('product_id')->unsigned();
            $table->string('coupon_code')->nullable();
            $table->timestamps();
        });
    
        Schema::table('carts', function($table) {
            $table->foreign('user_id')->references('id')->on('users');
            $table->foreign('product_id')->references('id')->on('products');
            $table->foreign('coupon_code')->references('code')->on('coupons');
        });
    }
    

    Validate coupon_code (IN carts controller for example):

    public function add_to_cart(Request $request)
    {
        $request->validate([
            'coupon_code' => 'nullable|exist:coupons,code',
        ]);
    
        if ($request->has('coupon_code')) {
    
             // get code here
             $code = $request->input('coupon_code');
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀