php - Last inserted record id cannot be retrieved in laravel

I'm trying to print last inserted record id of my stripe subscription table.
This is what I have done so far,
public function subscribe(Request $request)
{
$user = auth()->user();
$paymentMethod = $request->payment_method;
$appId = $request->appId;
$planId = $request->plan;
$user->newSubscription('main', $planId)->create($paymentMethod);
$AppPayment = DB::table('subscriptions')->insertGetId();
return $AppPayment;
dd($AppPayment);
$this->activateApp($appId);
}
The record has created but I'm not getting thedd
output..
Answer
Solution:
check your data array there is nothing insterting that is why id not getting
$dataArray = [
'column_name' => $request->inputFieldName,
];
$AppPayment = DB::table('subscriptions')->insertGetId($dataArray);
Answer
Solution:
you need to insert record here! This should be the place to insert the record.
$id = DB::table('subscriptions')->insertGetId(['foo'=>'bar']);
dd($id);
Update if you use cashier, check this code.
public function subscribe(Request $request)
{
$user = auth()->user();
$paymentMethod = $request->payment_method;
$appId = $request->appId;
$planId = $request->plan;
$subscription = $user->newSubscription('main', $planId)->create($paymentMethod);
dd($subscription->id);
return $AppPayment;
$this->activateApp($appId);
}
Answer
Solution:
This should work after you create the subscription:
$user->subscription->id;
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: uncaught mysqli_sql_exception
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.