Laravel5.8:插入数据库似乎不起作用

njthzxwz  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(312)

我在一家网上商店工作,想在里面插入一些付款细节 payments 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',
    ]);
}

因此,为了了解问题的根源,我尝试了以下方法:

if (!empty($existPayment)) {
    dd('Exists');
}

我得到了结果 Exists ,然后我试着:

if (!empty($existPayment)) {
        if($existPayment->delete()){
            dd('Deleted');
        }
    }

我再一次得到了正确的结果 Deleted ,之后我尝试:

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);
    }

我得到的结果是:

"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

但是当我检查数据库时,数据没有以某种方式插入,我不知道为什么!
我也把这个代码放进了 try..catch 但没有显示出任何错误。
所以,如果你知道如何解决这个问题或任何关于如何调试它的想法,请让我知道。。。
我会非常感激的。
提前谢谢。
型号:

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
    ];
    ...
rta7y2nd

rta7y2nd1#

试试这个

$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();

您还可以使用一种简单的方法,即使用fillable
在您的付款模式中添加

protected $fillable = [
    'pay_type_id ','pay_amount '   // all the columns you want 
];

并使用问题中提到的创建方法

$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',
    ]);

注意:必须在控制器中导入模型 use App\Payment;

相关问题