无法使用来自Model at Mail的功能 - laravel 5.1

时间:2016-03-27 12:05:13

标签: php laravel controller laravel-5.1

我有这个代码存储商品和maxoffer,但我不能将它用于我的邮件功能:

 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);
   if($maxoffer == null)
    {
      Auth::user()->maxoffer()->create($request->all());
    }
    else
    {
      if($maxoffer->price < $request->input('price'))
      {
        $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");
        $user = Auth::user();

        Mail::send('emails.newoffer', compact('user', 'maxoffer'), function ($m) use ($user) {
        $m->from('info@sss.com', $maxoffer->article()->hname);
        $m->to($user->email, $user->name)->subject('Someone have the bigger offer than you');
       });

        return Redirect::back();

    }

所以在Maxoffer控制器中我有:

public function user(){
        return $this->belongsTo('App\User');
    }

    public function article(){
        return $this->belongsTo('App\Article');
    }

但在邮件功能中我无法使用它。为什么?

为什么$maxoffer->article()->hname Mail::内部存在问题...

laravel错误:

我收到错误:22b7e7ff4b942f1d8fa25f9b1c9a1748第6行中的ErrorException:未定义属性:Illuminate \ Database \ Eloquent \ Relations \ BelongsTo :: $ hname(查看:/var/www/html/resources/views/emails/newoffer.blade.php )

1 个答案:

答案 0 :(得分:0)

将$ maxoffer传递给闭包。像这样使用($ user,$ maxoffer)

Mail::send('emails.newoffer', compact('user', 'maxoffer'), function ($m) use ($user, $maxoffer) {
    $m->from('info@sss.com', $maxoffer->article()->hname);
    $m->to($user->email, $user->name)->subject('Someone have the bigger offer than you');
   });