Php 如何在laravel中获取另一个表中的非整数列
我使用的是laravel 5.5,我的carts表迁移中有两列Php 如何在laravel中获取另一个表中的非整数列,php,mysql,laravel,Php,Mysql,Laravel,我使用的是laravel 5.5,我的carts表迁移中有两列优惠券和购物车,我有优惠券代码,它将从优惠券表中获取代码,结果将是购物车总价中的截止价格 这是我的购物车迁移: public function up() { Schema::create('carts', function (Blueprint $table) { $table->increments('id'); $table->integer('u
优惠券
和购物车
,我有优惠券代码
,它将从优惠券
表中获取代码,结果将是购物车总价中的截止价格
这是我的购物车迁移:
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');
});
}
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');
});
}
当我尝试运行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
我怎样才能解决这个问题
更新
优惠券
迁移
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();
});
}
主意
如果我在carts表中设置
优惠券\u代码
字符串并可为空,然后从优惠券表中验证优惠券代码,然后切断价格,该怎么办?你认为这样行吗?如果是的话,有人帮我定义这个范围吗?我注意到这两个字段都有不同的数据类型,例如在carts表中,优惠券代码字段是一个无符号整数,但在优惠券表中,代码字段是字符串
carts迁移:
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');
});
}
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');
});
}
验证优惠券\u代码(例如在购物车控制器中):
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');
}
}
我注意到这两个字段都有不同的数据类型,例如在carts表中,优惠券代码字段是无符号整数,但在优惠券表中,代码字段是字符串 carts迁移:
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');
});
}
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');
});
}
验证优惠券\u代码(例如在购物车控制器中):
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');
}
}
你能给我们看看你的优惠券吗。你所有的购物车都有优惠券代码吗?否则,您可能需要将carts表中的优惠券代码设置为空。请确保carts表中的优惠券代码与优惠券中的代码字段类型相同table@usrNotFound更新了我的问题,而且购物车中不会添加任何优惠券代码,这会影响购物车的总价格,而不是单一产品的价格。@YouneL您能更具体一点吗?@mafortis每个购物车都会有优惠券,是吗?您能给我们看一下您的优惠券表吗。你所有的购物车都有优惠券代码吗?否则,您可能需要将carts表中的优惠券代码设置为空。请确保carts表中的优惠券代码与优惠券中的代码字段类型相同table@usrNotFound更新了我的问题,而且不会向购物车中添加优惠券代码,这会影响购物车的总价格不是关于单个产品的。@您能说得更具体一些吗?@mafortis每个购物车都会有优惠券,对吗?它返回错误
一般错误:1215
。我也有一个想法,包括我的问题,请考虑一下,谢谢。我编辑了我的答案,你可以把空的方法添加到你的优惠券代码中,像是AbvoOK,现在是保存表,但是作为<代码> CoupOnLoad代码是字符串,我如何检查哪个用户将输入的是实际优惠券代码或只是打字?我的意思是需要对优惠券表进行某种验证,我该怎么做?您可以使用laravel检查用户输入是否与优惠券代码匹配,使用规则IT返回错误一般错误:1215
。我也有一个想法,包括我的问题,请考虑一下,谢谢。我编辑了我的答案,你可以把空的方法添加到你的优惠券代码中,像是AbvoOK,现在是保存表,但是作为<代码> CoupOnLoad代码是字符串,我如何检查哪个用户将输入的是实际优惠券代码或只是打字?我的意思是需要对优惠券表进行某种验证,我该怎么做?您可以使用laravel检查用户输入是否与优惠券代码匹配,使用规则