Skip to content

Instantly share code, notes, and snippets.

@mbpating
Created January 5, 2022 07:25
Show Gist options
  • Save mbpating/c8068f39f27c7775dc3ce4fe81d0de0a to your computer and use it in GitHub Desktop.
Save mbpating/c8068f39f27c7775dc3ce4fe81d0de0a to your computer and use it in GitHub Desktop.
Write a service class that handles saving and sending email then rewrite the store method.
<?php
namespace App\Http\Controllers;
use App\Http\Requests\CustomerRequest;
use App\Services\CustomerService;
class CustomerController extends Controller
{
public function store(CustomerRequest $request, CustomerService $service) {
$service->store($request->name,$request->email);
return [
'saved' => true
];
}
}
<?php
namespace App\Services;
use DB;
use Mail;
use App\Mail\WelcomeNewCustomer;
class CustomerService {
public function store($name,$email) {
$data = array (
'name' => $name,
'email' => $email
);
DB::table('customers')->insert($data);
Mail::to($email)->send(new WelcomeNewCustomer($data));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment