2

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 further.

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

I try to generate a mail with all the 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 = App\ModelSupplier\Supplier::all();
  return new App\Mail\Supplier\Certificates($suppliers);
});

Mail-Class:

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

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 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

Am I completely wrong?

Thank you in advance.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
T.Gerste
  • 41
  • 1
  • 8

2 Answers2

1

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!

T.Gerste
  • 41
  • 1
  • 8
0

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 Illuminate\Database\Eloquent\Collection;

class Certificates extends Mailable
{
    public $suppliers;

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

    public function build()
    {
        return $this->markdown('email.supplier.test');
    }
}
Chin Leung
  • 14,621
  • 3
  • 34
  • 58