我有这个功能:
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();
}
但是我无法添加数据,因为我得到了以下信息:
ErrorException in OffersController.php line 63: Undefined property:
Illuminate\Database\Eloquent\Collection::$price in
OffersController.php line 63 at HandleExceptions->handleError('8',
'Undefined property: Illuminate\Database\Eloquent\Collection::$price',
'C:\wamp\www\bidbook\app\Http\Controllers\OffersController.php', '63',
array('request' => object(OfferRequest), 'offer' => object(Offer),
'maxoffer' => object(Collection))) in OffersController.php line 63
这里有什么问题?
什么结果dd($ maxoffer)?
null .......................但是为什么,这是怎么回事:$ maxoffer = Maxoffer :: where(article_id,$ request-> input(article_id) )-> where(start,$ request-> input(start))-> get();
什么是dd($ request-> all());
数组:5 [▼" _token" =>" c8RwNaQY1smgPAMDmjCx8VP396RjemnVOoewHaqb"" article_id" =>" 1""键" =>" On6IH0PBQRewsOukcZkcdzsriqid5R""开始" =>" 02/16/2016 12:00"" 61.00"]
以下返回一个集合:
$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对象,并且在找到任何模型的情况下将起作用。
问题是因为我的$ request-> input(start)是2016-02-02 12:00 am,而从数据库开始是2016-02-02 00:00:00 ...如何解决此问题? 要用碳$ request-> input(start)转换?
您对该字段的数据类型是什么,希望它带有时间戳。
我也尝试使input(start)= 2016-02-02 00:00:00但又一次不起作用,只返回结果为NULL
尝试这个
$maxoffer = Maxoffer::where('article_id', $request->['article_id'])
->where('start', $request->['start'])->get();
嗯,不,那是相同的,但是然后我得到的不是数组,而是集合-在ControllerController.php第54行中出现FatalErrorException:语法错误,意外[,期望标识符(T_STRING)或变量(T_VARIABLE)或{或$
这个答案并不能解释您的想法是什么错,以及您的更改将如何解决它。 此外,它还会引入语法错误,这绝对不能解决该问题。