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

时间:2016-03-27 14:06:06

标签: php laravel controller undefined

我在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();

    }

所以,如果maxoffer不为空且如果maxoffer<request->input('price'),那么我需要更新一行并且工作正常,但我还需要将 MAIL 发送给以前的用户在新的maxoffer之前发布了maxoffer,但是在MAIL函数中我得到了:

未定义的变量:maxoffer documentation

这里有什么问题?为什么maxoffer未定义?

1 个答案:

答案 0 :(得分:2)

将$ maxoffer传递给函数闭包。使用($ 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')]);


   });