2016-02-13 82 views
0

添加数据我有这样的功能:无法从请求与laravel

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在OffersController.php线63: undefined property: 照亮\ Database \ Eloquent \ Collection :: $价格 OffersController.php第63行HandleExceptions-> handleError('8', '未定义的属性:Illuminate \ Database \ Eloquent \ Collection :: $ price', 'C:\ wamp \ www \ bidbook \ app \ Htt p \控制器\ OffersController.php”, '63', 阵列( '请求'=>对象(OfferRequest), '提供'=>对象(优惠), 'maxoffer'=>对象(集合)))在OffersController .PHP线63

这里有什么问题?

+0

什么结果DD($ maxoffer)? – paranoid

+0

null .......................但为什么,错在哪里:$ maxoffer = Maxoffer :: where('article_id',$ request-> input ( 'article_id的')) - >在哪里( '开始',$请求 - >输入( '开始')) - >获得(); – MonkeyBusiness

+1

以及结果dd($ request-> all()); – paranoid

回答

0

试试这个

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

hm,no,这是相同的,但后来我得到的集合不是数组--FreeErrorException中的OffersController.php第54行: 语法错误,意外的'[',期待标识符(T_STRING)或变量(T_VARIABLE)或'{'或'$' – MonkeyBusiness

+0

这个答案并不能解释你认为什么是错的,以及为什么你的改变会修复它。此外,它引入了语法错误,这肯定不会解决问题。 – patricus

1

下,返回集合:

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

所以,你得到的错误。只要改变getfirst例如:

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

现在,这会返回一个Eloquent Model对象,如果任何模型被发现会工作。

+0

问题是becouse我的$请求 - >输入(“启动”)是2016年2月2日上午12:00,并从数据库开始是2016年2月2日00:00:00 ...如何解决这个问题?用碳$ request-> input('start')转换? – MonkeyBusiness

+1

什么是您的字段的数据类型,希望它是时间戳。 –

+0

是,offcource TS时间戳... – MonkeyBusiness