首页 文章

无法使用 laravel 添加请求中的数据

提问于
浏览
0

我有这个功能:

public function store(Requests\OfferRequest $request)
    {

            $offer = new Offer($request->all());

            Auth::user()->offer()->save($offer);

            $maxoffer =  Maxoffer::where('article_id', $request->input('article_id'))
                        ->where('start', $request->input('start'))->get();

   if($maxoffer == null)
    {
      Auth::user()->maxoffer()->create($request->all());
    }
    else
    {
      if($maxoffer->price < $request->input('price'))
      {
        $newOffer = Auth::user()->maxoffer()
                    ->where('id', $maxoffer->id)
                    ->update(['price'=>$request->input('price')]);
      }
    }

        Alert::success('Offer is succesfully added!', 'Good job!')->persistent("Close");

        return Redirect::back();

    }

但我无法添加数据,因为我得到了这个:

OffersController.php 中的 ErrorException 63:Undefined 属性:Illleate\Database\Eloquent\Collection::$price in OffersController.php line 63 at HandleExceptions-> handleError('8','Undefined property:IlluminateDatabaseEloquentCollection::$price','C: wampwwwbidbookappHttpControllersOffersController.php','63',数组('request'=> object(OfferRequest),'offer'=> object(Offer),'maxoffer'=> object(Collection))) in OffersController.php line 63

这里有什么问题?

2 回答

  • 1

    以下内容返回一个集合:

    $maxoffer =  Maxoffer::where('article_id', $request->input('article_id'))
                        ->where('start', $request->input('start'))
                        ->get(); // Returns a collection
    

    所以你得到了错误。只需将get更改为first,例如:

    $maxoffer =  Maxoffer::where('article_id', $request->input('article_id'))
                        ->where('start', $request->input('start'))
                        ->first(); // Returns a single Eloquent Object
    

    现在,这将返回一个Eloquent Model对象,如果找到任何模型,它将起作用。

  • 0

    试试这个

    $maxoffer =  Maxoffer::where('article_id', $request->['article_id'])
                        ->where('start', $request->['start'])->get();
    

相关问题