php - 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
我完全错了吗?
先感谢您。
解决方案
好吧!这似乎是邮件类中的解决方案:
public function build()
{
$suppliers = Supplier::all();
return $this->markdown('email.supplier.certificates')->with(['suppliers'=>$suppliers]);
}
但我仍然愿意寻求更好的解决方案!
推荐阅读
- python-3.x - Peewee,如何避免具有多对多关系的 n+1 个查询
- vb.net - Image.Save() 为从位图派生的图像引发 GDI+ 异常
- python - 创建自定义层并对其进行卷积
- spring-boot - Hibernate Search 的外部化配置
- ms-access - 微软访问查询 IIF 语句没有返回我想要的
- ruby-on-rails - 如何使用 Rails 5 的 ActiveRecord 属性提供虚拟列
- maven - Gradle 构建失败,但 Maven 工作
- google-cloud-firestore - 虚拟钱包的 Firestore 幂等性
- routing - webpack 生产版本的“无法获取 /URL”错误,尽管使用 webpack 开发服务器它工作正常
- grafana - 在 Grafana - InfluxDB 数据源中将 count(null) 设为零