0

我一直在尝试编辑记录。如果数据为空,我的代码将创建一条新记录。但是,我收到以下错误:

fill()在 null 上调用成员函数。

我不确定我做错了什么;也许我没有声明?

控制器

<?php

public function auctionUpdate(Request $request, MediaSite $mediaSite)
{
    $auction = $mediaSite->auction;

    DB::transaction(function() use ($request, $mediaSite, $auction){
        $auction->fill($request->only([
           'status', 'start_time', 'end_time', 'period_start_date'
        ]));

        if($auction == null)
            $auction = new Auction();

        $auction->save();
   });

   return view('admin.media-site.show', [
       'mediaSite' => $mediaSite,
       'auction' => $auction
   ]);
}
4

1 回答 1

1

您应该在 fill() 之前检查拍卖是否为空

你修改的脚本

public function auctionUpdate(Request $request, MediaSite $mediaSite)
{
    $auction = $mediaSite->auction;

    DB::transaction(function() use ($request, $mediaSite, $auction){
        if($auction == null)
            $auction = new Auction();

        $auction->fill($request->only([
           'status', 'start_time', 'end_time', 'period_start_date'
        ]));

        $auction->save();
   });



   return view('admin.media-site.show', [
       'mediaSite' => $mediaSite,
       'auction' => $auction
   ]);
}
于 2018-11-28T04:00:48.950 回答