首页 > 解决方案 > Laravel 5.6 - 在降价中使用 @foreach

问题描述

我尝试在 Markdown 模板中呈现 @foreach 循环。但我不让它工作。我在 markdown mail 中找到了这个 Laravel-use @foreach,但它并没有让我走得更远。

我研究了 Laravel 文档,但似乎找不到我的问题。

我尝试使用供应商表中的所有信息生成一封邮件。因此我使用供应商类。

也许有人可以睁开我的眼睛,或者可以给我一个正确方向的提示。

路线:

Route::get('/mail',function(){

  $suppliers = App\ModelSupplier\Supplier::all();
  return new App\Mail\Supplier\Certificates($suppliers);
});

邮件等级:

namespace App\Mail\Supplier;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

 use App\ModelSupplier\Supplier;

class Certificates extends Mailable
{
 use Queueable, SerializesModels;

 public $supplier;

public function __construct(Supplier $supplier)
    {
    //
    $this->supplier = $supplier;

    }

public function build()
   {

        return $this->markdown('email.supplier.test');
    }
}

降价文件:

# Certificate:
@component('mail::table')
|No. | Company | Address
|:--------|:--------|----------:

@foreach($supplier as $detail)
| {{$detail->no}} | {{$detail->company}} | {{$detail->address}}
@endforeach
@endcomponent

我收到此错误:

 Argument 1 passed to App\Mail\Supplier\Certificates::__construct() 
 must be an instance of App\ModelSupplier\Supplier, instance of 
 Illuminate\Database\Eloquent\Collection given, called in C:\xampp\htdocs  
 \pps\routes\mail.php on line 7

我完全错了吗?

先感谢您。

标签: phpforeachmarkdownlaravel-5.6

解决方案


好吧!这似乎是邮件类中的解决方案:

public function build()
{

    $suppliers = Supplier::all();

    return $this->markdown('email.supplier.certificates')->with(['suppliers'=>$suppliers]);
}

但我仍然愿意寻求更好的解决方案!


推荐阅读