php artisan make:model PelangganBerita -m
- Open Model PelangganBerita
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePelangganBeritaTable extends Migration
{
public function up()
{
Schema::create('pelanggan_berita', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('nama');
$table->string('email')->unique();
$table->string('confirmation_code')->nullable();
$table->boolean('active')->default(0);
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('pelanggan_berita');
}
}
<form method="POST" action="/berlangganan-berita">
@csrf
<div class="row clearfix">
<div class="col-lg-9 col-md-9 col-sm-12 col-xs-12">
<div class="row clearfix">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div class="form-group">
<span class="floated-icon fa fa-user"></span>
<input type="text" name="nama" value="" placeholder="Nama Anda" required>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div class="form-group">
<span class="floated-icon fa fa-envelope"></span>
<input type="email" name="email" value="" placeholder="Email Anda" required>
</div>
</div>
</div>
</div>
<div class="col-lg-3 col-md-3 col-sm-12 col-xs-12">
<button type="submit" class="theme-btn">Berlangganan</button>
</div>
</div>
</form>
Route::post('/berlangganan-berita', 'Welcome\WelcomeController@berlanggananBerita');
Route::get('/berlangganan-berita/verify/{token}', 'Welcome\WelcomeController@berlanggananVerify');
<?php
namespace App\Http\Controllers\Welcome;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\PelangganBerita;
use Session, Redirect, Validator;
use Mail;
use App\Mail\MailVerification;
class WelcomeController extends Controller
{
public function berlanggananBerita(Request $request)
{
$messages = [
'nama.required' => ':attribute harus diisi.',
'email.required' => ':attribute harus diisi.',
'email.unique' => ':attribute sudah pernah terdaftar. harap gunakan email lain!.',
];
$validator = Validator::make($request->all(), [
'nama' => ['required'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:pelanggan_berita'],
], $messages);
if ($validator->fails()) {
return redirect('/alert/status')
->withErrors($validator)
->withInput();
}
$confirmation_code = uniqid('TUTA') . str_random(30);
$pelangganBerita = new PelangganBerita;
$pelangganBerita->nama = $request->nama;
$pelangganBerita->email = $request->email;
$pelangganBerita->confirmation_code = $confirmation_code;
$pelangganBerita->save();
Mail::to($pelangganBerita->email)->later(10, new MailVerification($pelangganBerita));
Session::flash('message', 'Selamat anda sudah mendaftar, cek email Anda untuk melakukan verifikasi email!');
return Redirect::to('/pertanyaan/status');
}
public function berlanggananVerify($token) {
$pelangganBerita = PelangganBerita::where('confirmation_code', $token)
->where('active', 0)->firstOrFail();
$pelangganBerita->active = 1;
$pelangganBerita->save();
Session::flash('message', 'Selamat email anda sudah diaktifkan untuk menerima Berita!');
return Redirect::to('/alert/status');
}
}
php artisan make:mail MailVerification
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class MailVerification extends Mailable
{
use Queueable, SerializesModels;
protected $subscribe;
public function __construct($subscribe)
{
$this->subscribe = $subscribe;
}
public function build()
{
$data = $this->subscribe;
return $this->view('emails.verification', compact('data'));
}
}
- make view emails/verification.blade.php
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
</head>
<body>
<h2>Konfirmasi Alamat Email Anda</h2>
<div>
Email ini adalah atas pendaftaran anda di Website Mitra Multi Teknologi<br />
Untuk berlangganan Berita.
<p>Silahkan klik link dibawah ini untuk konfirmasi alamat email anda</p>
<p><a href="{{ URL::to('berlangganan-berita/verify/' . $data->confirmation_code) }}">{{ URL::to('berlangganan-berita/verify/' . $data->confirmation_code) }}</a></p>
<br /><br />
<em>*Abaikan email ini jika anda tidak merasa mendaftar untuk berlangganan berita<br />
pada Mitra Multi Teknologi</em>
<br />
<p>NB:</p>
<p>=====*Ini hanya email pemberitahuan=====</p>
<p>=====*Harap tidak membalas email ini=====</p>
</div>
</body>
</html>