php - Laravel 5.8: Insertion into database does not seem to be working
I am working on an Online Store and want to insert some payment details into payments
table.
Everything works good except this insertion process that does insert data into table:
if (!empty($existPayment)) {
$existPayment->delete();
$payment = Payment::create([
'pay_type_id' => '186',
'pay_date' => jdate()->format('Y/m/d'),
'pay_amount' => $order->ord_total * 10,
'pay_ord_id' => $order->ord_id,
'pay_status_id' => '182',
'pay_creator_id' => auth()->user()->usr_id,
'pay_confirm' => '1',
]);
}
So in order to know where the problem is coming from, I tried this:
if (!empty($existPayment)) {
dd('Exists');
}
And I got the result Exists
, then I tried:
if (!empty($existPayment)) {
if($existPayment->delete()){
dd('Deleted');
}
}
And again I got the correct result which is Deleted
, after that I tried:
if (!empty($existPayment)) {
$existPayment->delete();
$payment = Payment::create([
'pay_type_id' => '186',
'pay_date' => jdate()->format('Y/m/d'),
'pay_amount' => $order->ord_total * 10,
'pay_ord_id' => $order->ord_id,
'pay_status_id' => '182',
'pay_creator_id' => auth()->user()->usr_id,
'pay_confirm' => '1',
]);
dd($payment);
}
And I got this as result:
"pay_type_id" => "186"
"pay_date" => "1400/05/05"
"pay_amount" => 750000.0
"pay_ord_id" => 9222
"pay_status_id" => "182"
"pay_creator_id" => 2
"pay_confirm" => "1"
"updated_at" => "2021-07-27 12:12:06"
"created_at" => "2021-07-27 12:12:06"
"pay_id" => 8929
But when I check the DB, the data is not inserted somehow and I don't know why!
I also put this code in try..catch
but didn't show any kinds of error.
So if you know how to solve this issue or any idea on how to debug it, please let me know...
I would really appreciate that.
Thanks in advance.
MODEL:
class Payment extends Model
{
use SoftDeletes;
protected $table = "payments";
protected $primaryKey = "pay_id";
protected $guarded = [];
protected $appends = [
'status_label', 'type_label'
];
protected $fillable = [
'pay_type_id ','pay_date','pay_amount','pay_ord_id','pay_status_id','pay_creator_id','pay_confirm' // all the columns you want
];
...
Answer
Solution:
Try This
$payment = new Payment;
$payment->pay_type_id = '186';
$payment->pay_date = jdate()->format('Y/m/d');
$payment->pay_amount = $order->ord_total * 10;
$payment->pay_ord_id = $order->ord_id;
$payment->pay_status_id = '182';
$payment->pay_creator_id = auth()->user()->usr_id;
$payment->pay_confirm = '1';
$payment->save();
You can Also Use an Easy Way By Using Fillable
For that In Your Payment Model add
protected $fillable = [
'pay_type_id ','pay_amount ' // all the columns you want
];
And use the Create method Mentioned In the Question
$payment = Payment::create([
'pay_type_id' => '186',
'pay_date' => jdate()->format('Y/m/d'),
'pay_amount' => $order->ord_total * 10,
'pay_ord_id' => $order->ord_id,
'pay_status_id' => '182',
'pay_creator_id' => auth()->user()->usr_id,
'pay_confirm' => '1',
]);
Note the Model Must be imported In Controller use App\Payment;