Massive Mailings with laravel 5.5

0

Good afternoon I have a problem with sending bulk emails, I'm working with two tables that is "mailings" and "customers", and I get this error

1- The route is this. routes / web.php

Route::get('mailings/messages', 'MailingsController@messages')->name('admin.mailing.messages');

2- This is the model app / Mailing.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Mailing extends Model
{
    protected $fillable = [
        'porcentage', 'name', 'price', 'content', ' avatar', 'valid_month', 'cellphone01', 'cellphone02', 'phone', 'email'
    ];

    public function customers()
    {
        return $this->hasMany(Customer::class);
    }


}

3- This is the app / Customer.php model

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    protected $fillable = [
        'name', 'email', 'address', 'phone'
    ];

    public function mailings()
    {
        return $this->belongsTo(Mailing::class);
    }
}

4- This is the app / Http / controllers / Admin / CustosmersController.php driver

public function messages(){

    $mailings = Mailing::all();

    Customer::chunk(200, function ($customers){
        foreach ($customers as $customer){
            Mail::send('emails.message', ['customer' => $customer], function ($message) use ($customer){
                $message->from('[email protected]', 'Elvin Mejía Paucar');
                $message->to($customer->email, $customer->name)->subject('Tenemos novedades para ti ' . $customer->name);
            });
        }
    });

    return back()->with('flash', ' Tu mensaje ha sido recibido con éxito', compact('mailings'));

}

5- This is the view message.blade.php

@foreach($mailings as $mailing)
    <table style="width: 100%;">
        <tr>
            <td style="text-align: center;">
                <h1 style="margin: 0px; font-size: 48px; text-align: center; margin: 0px; color: #fff;">
                    <strong>{{ $mailing->porcentage }}% de descuento</strong>
                </h1>
            </td>
        </tr>
        <tr>
            <td>
                <h3 style="font-size: 24px; color: #fff; text-align: center; margin: 0; line-height: 11px;">{{ $mailing->name }}</h3>
            </td>
        </tr>
        <tr>
            <td>
                <h4 style="text-decoration:line-through; color: #fff; text-align: center; margin: 0;">Antes: {{ $mailing->price }}. 00</h4>
            </td>
        </tr>
    </table>
@endforeach

I would appreciate if you can help me please

Customers table

Table mailings

    
asked by Elvin Mejía 27.07.2018 в 07:35
source

1 answer

0

The problem is because you are going back to the previous page but that data is passed through session variables.

Instead of:

return back()->with('flash', ' Tu mensaje ha sido recibido con éxito', compact('mailings'));

You should return the view and pass the data there:

return view('ruta.a.vista', compact("mailings"))->with('flash', 'Tu mensaje ha sido recibido con éxito') ;

    
answered by 27.07.2018 в 21:41