我有这个功能:
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 回答
以下内容返回一个集合:
所以你得到了错误。只需将
get
更改为first
,例如:现在,这将返回一个
Eloquent Model
对象,如果找到任何模型,它将起作用。试试这个