簡體   English   中英

無法使用Mail的模型功能-Laravel 5.1

[英]Can't use function from Model at Mail - laravel 5.1

我有用於存儲offer和maxoffer的這段代碼,但是我無法在Mail函數中使用它:

 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');
    }

但是在郵件功能中,我無法使用它。 為什么?

為什么Mail::中的$maxoffer->article()->hname是一個問題...

laravel錯誤:

我得到錯誤:22b7e7ff4b942f1d8fa25f9b1c9a1748第6行中的ErrorException:未定義的屬性:Illuminate \\ Database \\ Eloquent \\ Relations \\ BelongsTo :: $ hname(查看:/var/www/html/resources/views/emails/newoffer.blade.php)

將$ 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');
   });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM