简体   繁体   中英

Save to user database from a Json response in Laravel

I successfully implement a payment gateway on my Laravel Application

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 set sub_paid_at = ?, users . updated_at = 2019-12-14 07:27:45 where id = 3)

UPDATE

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 set role = ?, sub_paid_at = 1970-01-01 00:00:00, users . updated_at = 2019-12-14 08:40:46 where id = 3)

Change your variable name from paid_at to 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM