更新不起作用时 Laravel 保存(奇怪的行为)

Posted

技术标签:

【中文标题】更新不起作用时 Laravel 保存(奇怪的行为)【英文标题】:Laravel save when update not working (strange behavior) 【发布时间】:2021-03-30 13:36:29 【问题描述】:

我正在使用 Laravel 8,我尝试更新数据,但它不会保存,这真的很奇怪。

控制器

private function _generatePaymentToken($kode)

    $this->initPaymentGateway();
    $pnd = new Pendaftar;
    $pnd = $pnd::where('nohp', $kode)->get();
    if ($pnd->count()) 
        $customerDetails = [
            'first_name' => $pnd[0]->nama,
            'last_name' => $pnd[0]->nama,
            'email' => $pnd[0]->email,
            'phone' => $pnd[0]->nohp,
        ];

        $params = [
            'transaction_details' => [
                'order_id' => $pnd[0]->kode_transaksi,
                'gross_amount' => 5000,
            ],
            'customer_details' => $customerDetails,
            'expiry' => [
                'start_time' => date('Y-m-d H:i:s T'),
                'unit' => setting('site.unit_expire'),
                'duration' => setting('site.time_expired'),
            ],
        ];

        $snap = \Midtrans\Snap::createTransaction($params);

        $pendaftar = Pendaftar::find($pnd[0]->id);

        $pendaftar->token = $snap->token;
        $pendaftar->save;

        // dd($pendaftar->save);
        return $snap;

     else 
        return view('404');
    

型号

namespace App;

use Illuminate\Database\Eloquent\Model;


class Pendaftar extends Model

    protected $table = 'pendaftar';
    protected $primary_key = 'id';
    protected $fillable = ['nama, email, nohp,orangtuawali, alamat, tempat_tanggal_lahir,asal_sekolah, satutahun_regular, satutahun_hybrid,duatahun, sarjana_plus, ihcp, ecp, periode_pendaftaran, created_at, updated_at, deleted_at, kode_refrensi, kode_transaksi,kampus,token'];

返回前的$pendaftar->save 不起作用

我尝试dd($pendaftar->save)它给我null

如果我 dd($pendaftar) 它会给我这个

App\Pendaftar #1840
  #table: "pendaftar"
  #primary_key: "id"
  #fillable: array:1 [
    0 => "nama, email, nohp,orangtuawali, alamat, tempat_tanggal_lahir,asal_sekolah, satutahun_regular, satutahun_hybrid,duatahun, sarjana_plus, ihcp, ecp, periode_pendaftaran, created_at, updated_at, deleted_at, kode_refrensi, kode_transaksi,kampus,token"
  ]
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:22 [
    "id" => "6"
    "nama" => "Putu Elanda"
    "email" => "biruxxx@mail.com"
    "nohp" => "0878xxx"
    "orangtuawali" => "Nyoman Elendiana"
    "alamat" => "Singaraja"
    "tempat_tanggal_lahir" => "Singaraja / 10 Januari 1996"
    "asal_sekolah" => "SMK N 3 Singaraja"
    "satutahun_regular" => null
    "satutahun_hybrid" => null
    "duatahun" => null
    "sarjana_plus" => null
    "ihcp" => "Profesi 2 Tahun "
    "ecp" => "Food Entrepreneurship and Culinary Art"
    "periode_pendaftaran" => "Januari"
    "created_at" => "2020-12-20 14:15:03"
    "updated_at" => "2020-12-20 14:15:03"
    "deleted_at" => null
    "kode_refrensi" => "BHT08"
    "kode_transaksi" => "2012POSA"
    "kampus" => null
    "token" => "9067715d-aa54-4b10-a457-5445f06df3c4"
  ]
  #original: array:22 [
    "id" => "6"
    "nama" => "Putu Elanda"
    "email" => "biruxxx@mail.com"
    "nohp" => "0878xxx"
    "orangtuawali" => "Nyoman Elendiana"
    "alamat" => "Singaraja"
    "tempat_tanggal_lahir" => "Singaraja / 10 Januari 1996"
    "asal_sekolah" => "SMK N 3 Singaraja"
    "satutahun_regular" => null
    "satutahun_hybrid" => null
    "duatahun" => null
    "sarjana_plus" => null
    "ihcp" => "Profesi 2 Tahun "
    "ecp" => "Food Entrepreneurship and Culinary Art"
    "periode_pendaftaran" => "Januari"
    "created_at" => "2020-12-20 14:15:03"
    "updated_at" => "2020-12-20 14:15:03"
    "deleted_at" => null
    "kode_refrensi" => "BHT08"
    "kode_transaksi" => "2012POSA"
    "kampus" => null
    "token" => ""
  ]
  #changes: []
  #casts: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [
    0 => "*"
  ]

我真的不知道这有什么问题,或者我想念什么?

【问题讨论】:

这个$pendaftar->save;保存应该是一个函数$pendaftar->save(); 天哪,我想我现在应该去睡觉了。谢谢 【参考方案1】:

您的 $fillable 数组定义不正确 - 每个列名都应该是数组的一个元素 - 用引号括起来。否则不允许任何字段进行批量分配。

这意味着即使更新或保存方法不会引发错误 - 这些列的数据也不会保存到数据库中。

namespace App;

use Illuminate\Database\Eloquent\Model;


class Pendaftar extends Model

    protected $table = 'pendaftar';
    protected $primary_key = 'id';
    protected $fillable = [
        'nama',
        'email',
        'nohp',
        'orangtuawali',
        'alamat',
        'tempat_tanggal_lahir',
        'asal_sekolah',
        'satutahun_regular',
        'satutahun_hybrid',
        'duatahun', 
        'sarjana_plus',
        'ihcp',
        'ecp',
        'periode_pendaftaran',
        'created_at',
        'updated_at',
        'deleted_at',
        'kode_refrensi',
        'kode_transaksi',
        'kampus,token'
    ];

正如@bhucho 所指出的,save() 是一种方法而不是属性,所以声明应该是

$pendaftar->save();

【讨论】:

以上是关于更新不起作用时 Laravel 保存(奇怪的行为)的主要内容,如果未能解决你的问题,请参考以下文章

在 MySQL 中更新记录时,Laravel 的 useCurrent() 不起作用

NavigationBar 后退按钮奇怪的行为并且不起作用

使用ajax调用的角度保存变量到laravel会话不起作用

奇怪的行为:从堆栈拖动到状态项不起作用

Laravel 收银员数量增量更新不起作用

在 Mongo Db 中更新相关文档后 Spring 用户身份验证不起作用