【问题标题】:CAN'T UPDATE DATA IN LARAVEL CONTROLLER无法更新 LARAVEL 控制器中的数据
【发布时间】:2020-01-31 15:30:03
【问题描述】:

我无法将数据更新到数据库,我使用邮递员,结果是成功的,但我的数据库中的数据没有更新。

这是我在控制器中的功能:

public function update(Request $request, $id) {
            $dbelicpkb = DBeliCPKB::find($id);
            if(is_null($dbelicpkb)) {
                return response()->json('Spesifikasi Not Found', 404);
            }
            if($request->tanggal != $dbelicpkb["Tanggal"]){
                $dbelicpkb["Tanggal"] = $request->tanggal;
            }
            if($request->noSuplier != $dbelicpkb["No Suplier CPKB"]){
                $dbelicpkb["No Suplier CPKB"] = $request->noSuplier;
            }
            if($request->kodeBarang != $dbelicpkb["Kode Barang CPKB"]){
                $dbelicpkb["Kode Barang CPKB"] = $request->kodeBarang;
            }
            if($request->jumlah != $dbelicpkb["Jumlah"]){
                $dbelicpkb["Jumlah"] = $request->jumlah;
            }
            if($request->noLP != $dbelicpkb["No LP"]){
                $dbelicpkb["No LP"] = $request->noLP;
            }
            if($request->noBet != $dbelicpkb["No Bet"]){
                $dbelicpkb["No Bet"] = $request->noBet;
            }
            if($request->noFaktur != $dbelicpkb["No Faktur"]){
                $dbelicpkb["No Faktur"] = $request->noFaktur;
            }
            if($request->status != $dbelicpkb["Status"]){
                $dbelicpkb["Status"] = $request->status;
            }
            $success = $dbelicpkb->save(); 

这是我尝试使用 POSTMAN 时的结果

[
    "updated",
    {
        "Tanggal": "2020-01-31 00:00:00",
        "No Bukti CPKB": "2001001",
        "No Suplier CPKB": "300001",
        "Kode Barang CPKB": "1010",
        "Jumlah": "20",
        "No LP": "P2001001",
        "No Bet": "101010",
        "No Uji": "UP2001001",
        "No Faktur": "1010",
        "Status": "Belum Diuji"
    },
    true,
    200
]

结果是成功,但我的数据库中没有任何反应

这是我的路线

Route::post('dbelicpkbs/update/{id}', 'DBeliCPKBController@update');

这是我的模型

class DBeliCPKB extends Model
{
     /**
     * @var string
     */
    protected $table = 'dbelicpkb';
    protected $primaryKey = 'no uji';
    /**
     * @var array
     */
    protected $guarded = [];

    public $timestamps = false;

    public function HBeliPembelians()
    {
        return $this->belongsTo(HBeliPembelian::class,'No Bukti');
    }

}

【问题讨论】:

  • 你的主键怎么会有空格?
  • 我强烈建议重命名您的列名。带有空格的列名只会给您将来带来更多问题。
  • 已解决!我在这里得到了解决方案stackoverflow.com/questions/34458985/…

标签: laravel


【解决方案1】:

首先,在列名中包含空格是非常糟糕的做法。

您必须使用箭头来设置给定模型实例的属性:

public function update(Request $request, $id) {
    $dbelicpkb = DBeliCPKB::find($id);
    if(is_null($dbelicpkb)) {
        return response()->json('Spesifikasi Not Found', 404);
    }
    if($request->tanggal != $dbelicpkb->{'Tanggal'}){
        $dbelicpkb->{'Tanggal'} = $request->tanggal;
    }
    if($request->noSuplier != $dbelicpkb->{'No Suplier CPKB'}){
        $dbelicpkb->{'No Suplier CPKB'} = $request->noSuplier;
    }
    if($request->kodeBarang != $dbelicpkb->{'Kode Barang CPKB'}){
        $dbelicpkb->{'Kode Barang CPKB'} = $request->kodeBarang;
    }
    if($request->jumlah != $dbelicpkb->{'Jumlah'}){
        $dbelicpkb->{'Jumlah'} = $request->jumlah;
    }
    if($request->noLP != $dbelicpkb->{'No LP'}){
        $dbelicpkb->{'No LP'} = $request->noLP;
    }
    if($request->noBet != $dbelicpkb->{'No Bet'}){
        $dbelicpkb->{'No Bet'} = $request->noBet;
    }
    if($request->noFaktur != $dbelicpkb->{'No Faktur'}){
        $dbelicpkb->{'No Faktur'} = $request->noFaktur;
    }
    if($request->status != $dbelicpkb->{'Status'}){
        $dbelicpkb->{'Status'} = $request->status;
    }
    $success = $dbelicpkb->save(); 
}

还要确保您已在模型的 $fillable 属性中分配了这些列:

public $fillable = [
    'No LP',
    'No Bet'
    ...
];

或者通过在你的模型中放置 protected $guarded = []; 来使所有东西都可以填充

希望这个答案对你有所帮助。

【讨论】:

  • 可填充应该不是问题,因为它不是批量分配
  • 首先,谢谢你的回答,但它仍然对我不起作用..
  • 你能用where条件试试吗? DBeliCPKB::where('id', $id)->update([ 'some_key', 'some value']);
【解决方案2】:

试试这个:

方法和编码似乎有问题。

使用 x-www-form-urlencoded 放置

应该有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 2019-03-21
    • 2016-12-12
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    相关资源
    最近更新 更多