Skip to content

Instantly share code, notes, and snippets.

@victorsteven
Created April 15, 2019 16:38
Show Gist options
  • Save victorsteven/e22005061d1930d56d27bd3b0b27c65c to your computer and use it in GitHub Desktop.
Save victorsteven/e22005061d1930d56d27bd3b0b27c65c to your computer and use it in GitHub Desktop.
Message Controller file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Message;
use App\Jobs\SendMailJob;
use Carbon\Carbon;
use App\User;
use App\Mail\NewArrivals;
class MessageController extends Controller
{
public function getUsers(){
return User::all();
}
public function getMessages(){
return Message::orderBy('created_at', 'desc')->get();
}
public function sendMail(Request $request)
{
$message = new Message();
$message->title = $request->title;
$message->body = $request->body;
$message->save();
if($request->item == "now") {
$message->delivered = 'YES';
$message->send_date = Carbon::now();
$message->save();
$users = User::all();
foreach($users as $user) {
dispatch(new SendMailJob($user->email, new NewArrivals($user, $message)));
}
return response()->json('Mail sent.', 201);
} else {
$message->date_string = date("Y-m-d H:i", strtotime($request->send_date));
$message->save();
return response()->json('Notification will be sent later.', 201);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment