doujizhong8352
doujizhong8352
2017-11-06 00:24

如何在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 drpp5680 4年前

    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');
    
        }
    }
    
    点赞 评论 复制链接分享