/** * {@inheritdoc} */ public function recent($user) { // Retrieve all unread notifications for the user... $unreadNotifications = Notification::with('creator')->where('user_id', $user->id)->where('read', 0)->orderBy('created_at', 'desc')->get(); // Retrieve the 8 most recent read notifications for the user... $readNotifications = Notification::with('creator')->where('user_id', $user->id)->where('read', 1)->orderBy('created_at', 'desc')->take(8)->get(); // Add the read notifications to the unread notifications so they show afterwards... $notifications = $unreadNotifications->merge($readNotifications)->sortByDesc('created_at'); if (count($notifications) > 0) { Notification::whereNotIn('id', $notifications->lists('id'))->where('user_id', $user->id)->where('created_at', '<', $notifications->first()->created_at)->delete(); } return $notifications->values(); }
<?php Route::group(['middleware' => ['web', 'dev']], function ($router) { $router->get('/skn/notifications', function () { return \Laravel\Spark\Notification::with('creator')->with('user')->orderBy('created_at', 'desc')->get(); }); $router->post('/skn/notifications/create', function (Illuminate\Http\Request $request) { $new_notification = new \Laravel\Spark\Notification(); // Must generate an unique id since it's not auto increment $new_notification->id = Illuminate\Support\Facades\Hash::make(time() . Auth::user()->id); $new_notification->body = $request->input('body'); $new_notification->user_id = $request->input('user_id'); $new_notification->action_text = $request->input('action_text'); $new_notification->action_url = $request->input('action_url'); $new_notification->created_by = Auth::user()->id; $new_notification->save(); return $new_notification; }); $router->get('/skn/users', function () { return \App\User::all(); }); });