I have this function:
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();
}
but I can't add data because I got this:
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
What is the problem here?
Try this
$maxoffer = Maxoffer::where('article_id', $request->['article_id'])
->where('start', $request->['start'])->get();
The following returns a collection:
$maxoffer = Maxoffer::where('article_id', $request->input('article_id'))
->where('start', $request->input('start'))
->get(); // Returns a collection
So you get the error. Just change the get
to first
for example:
$maxoffer = Maxoffer::where('article_id', $request->input('article_id'))
->where('start', $request->input('start'))
->first(); // Returns a single Eloquent Object
Now, this will return a single Eloquent Model
object and will work if any model was found.