Laravel 5.6 - Using @foreach in a markdown


Laravel 5.6 - Using @foreach in a markdown



Hello stackoverflowers,



I try to render a @foreach loop in a markdown template. But I don't get it to work. I found this Laravel- use @foreach in markdown mail, but it didn't take me futher.



I studied the laraval docs but it seems that I'm unable to find my issue.



I try to generate a mail with all information from the supplier-table. Therefore I use the Supplier Class.



Maybe someone could open my eyes or could give me a hint in the right direction.



Route:


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

$suppliers = AppModelSupplierSupplier::all();
return new AppMailSupplierCertificates($suppliers);
});



Mail-Class:


namespace AppMailSupplier;

use IlluminateBusQueueable;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;
use IlluminateContractsQueueShouldQueue;

use AppModelSupplierSupplier;

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



Markdown-File:


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

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



I'm receiving this error:


Argument 1 passed to AppMailSupplierCertificates::__construct()
must be an instance of AppModelSupplierSupplier, instance of
IlluminateDatabaseEloquentCollection given, called in C:xampphtdocs
ppsroutesmail.php on line 7



Am I completely wrong?



Thank you in advance.





@foreach is only for blade template. Its not about laravel.
– user254153
Jul 3 at 9:46




2 Answers
2



Alright! This seems to be the solution in the Mail-Class:


public function build()
{

$suppliers = Supplier::all();

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



But I'm still open for better solutions!





exactly .. as you are calling a view you should pass all the data that's used
– Athul Raj
Jul 3 at 14:27



You are expecting a single Supplier in your constructor, but you are giving it the collection of suppliers when you initialize it.


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



However, it should be something like this:


use IlluminateDatabaseEloquentCollection;

class Certificates extends Mailable
{
public $suppliers;

public function __construct(Collection $suppliers)
{
$this->suppliers = $suppliers;
}

public function build()
{
return $this->markdown('email.supplier.test');
}
}





Thank you very much!
– T.Gerste
Jul 6 at 9:48





@T.Gerste Np, can you mark my answer as the correct one please?
– Chin Leung
Jul 6 at 17:57






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?