[英]Save to user database from a Json response in Laravel
I successfully implement a payment gateway on my Laravel Application我成功地在我的 Laravel 应用程序上实现了一个支付网关
But I get this response from the payment但我从付款中得到了这个回应
public function handleGatewayCallback()
{
$paymentDetails = Payant::getPaymentData();
dd($paymentDetails);
}
What I am trying to is to save some of the response to the user database, but I am unable to achieve this.我想要的是将一些响应保存到用户数据库,但我无法实现这一点。 I tried doing it this way
我试过这样做
public function handleGatewayCallback(Request $request)
{
$paymentDetails = Payant::getPaymentData();
// dd($paymentDetails);
$user = User::find(Auth::id());
$user->sub_paid_at = $request->paid_at;
$user->role = $request->planObject['name'];
$user->save();
}
It returned this error它返回了这个错误
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'sub_paid_at' cannot be null (SQL: update
users
setsub_paid_at
= ?,users
.updated_at
= 2019-12-14 07:27:45 whereid
= 3)SQLSTATE [23000]:完整性约束违规:1048列'sub_paid_at'不能为空(SQL:更新
users
设置sub_paid_at
?=,users
。updated_at
=二○一九年十二月一十四日7点27分45秒,其中id
= 3)
This is my user database Schema这是我的用户数据库架构
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->enum('role', ['subscriber', 'admin', 'basic', 'couple', 'family'])->default('subscriber');
$table->timestamp('email_verified_at')->nullable();
$table->string('avatar')->nullable();
$table->integer('no_of_logins')->default(0);
$table->date('sub_paid_at')->nullable();
$table->string('session_id')->nullable();
$table->string('password')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
I want to be able to update user role from the response, so I tried this我希望能够从响应中更新用户角色,所以我尝试了这个
$user->role = $request->plan_object['name'];
But it returned error但它返回错误
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'role' cannot be null (SQL: update
users
setrole
= ?,sub_paid_at
= 1970-01-01 00:00:00,users
.updated_at
= 2019-12-14 08:40:46 whereid
= 3)SQLSTATE [23000]:完整性约束违规:1048列'角色'不能为空(SQL:更新
users
设置role
?=,sub_paid_at
= 1970-01-01 00:00:00,users
。updated_at
= 2019年12月14日08 :40:46 其中id
= 3)
Change your variable name from paid_at
to paidAt
将您的变量名称从
paid_at
更改为paidAt
$user->sub_paid_at = $request->paidAt;
But better you change format但最好你改变格式
$user->sub_paid_at = date("Y-m-d H:i:s",strtotime($request->paidAt));
This fix my question这解决了我的问题
$user = User::find(Auth::id());
$user->sub_paid_at = date("Y-m-d H:i:s",strtotime($paymentDetails['data']['paidAt']));
$user->role = $paymentDetails['data']['plan_object']['name'];
$user->save();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.