douxi8119
2016-03-27 14:06
浏览 75
已采纳

Laravel 5.1 - Mail函数中的未定义变量

I have this store method inside OfferController:

 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', Carbon::createFromFormat('m/d/Y h:i a', $request->input('start')))
                    ->first();

                    //dd($maxoffer->article()->first()->user->name);
   if($maxoffer == null)
    {
      Auth::user()->maxoffer()->create($request->all());
    }
    else
    {
      if($maxoffer->price < $request->input('price'))
      {
        $user = Auth::user();

        Mail::send('emails.newoffer', compact('user', 'maxoffer'), function ($m) use ($user) {

        $m->from($maxoffer->article()->first()->user->email, $maxoffer->article()->first()->user->name);
        $m->to($maxoffer->user()->first()->email, $maxoffer->user()->first()->name)->subject('Someone have the bigger offer than you');

        $key = '';
        $newOffer = Maxoffer::where('id', $maxoffer->id)
                    ->update(['price'=>$request->input('price'),'user_id'=>Auth::user()->id, 'key'=>$key, 'provera'=>$request->input('provera')]);


       });
      }
    }

        Alert::success('Keep looking for best rates. Good luck...', 'Thanks for bidding!')->persistent("Close");


        return Redirect::back();

    }

so if maxoffer is not null and if maxoffer<request->input('price') then I need to update an row and that work nice, but also I need to send MAIL to previous user which was posted maxoffer before new maxoffer but inside MAIL function I get just:

undefined variable: maxoffer enter image description here

what is problem here? Why maxoffer is undefined?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doqvzh345334 2016-03-27 14:10
    已采纳

    Pass $maxoffer to the function closure. use($user, $maxoffer)

     Mail::send('emails.newoffer', compact('user', 'maxoffer'), function ($m) use ($user, $maxoffer) {
    
        $m->from($maxoffer->article()->first()->user->email, $maxoffer->article()->first()->user->name);
        $m->to($maxoffer->user()->first()->email, $maxoffer->user()->first()->name)->subject('Someone have the bigger offer than you');
    
        $key = '';
        $newOffer = Maxoffer::where('id', $maxoffer->id)
                    ->update(['price'=>$request->input('price'),'user_id'=>Auth::user()->id, 'key'=>$key, 'provera'=>$request->input('provera')]);
    
    
       });
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题