Tell me which way to think.
I send mailing to several addresses at once. I form a view, which is sent to the addresses. Each email must contain a unique username from a select of all users in the model. Accordingly, each user should have their own view (with their name).
The question is - how to create a unique view for each email in the mailing list?
I cannot write this way, since all users will receive the same email with a list of all usernames:
$foreach ($users as $user)
{{ $user->name }}
@endforeach
And how to get result where each user get their name in the letter?
You need to send the email for each user individually, passing the user data to the mailable class you have created.
<?php
namespace App\Mail;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class MyCustomUserMail extends Mailable
{
use Queueable, SerializesModels;
public $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function build()
{
return $this->view('emails.users.my_custom_user_view');
}
}
In your custom view:
{{ $user->name }}
Sending the email:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Mail\MyCustomUserMail;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class UserController extends Controller
{
public function doSomething()
{
$users = User::all();
$users->each(function ($user) {
Mail::to($user->email)->send(new MyCustomUserMail($user));
});
}
}