Created
April 15, 2019 16:38
-
-
Save victorsteven/e22005061d1930d56d27bd3b0b27c65c to your computer and use it in GitHub Desktop.
Message Controller file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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