WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 968238e

Browse files
committed
updated show and update methods with exception handling, added flash error message to example view
1 parent c192dd0 commit 968238e

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/Cmgmyr/Messenger/examples/MessagesController.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
use Cmgmyr\Messenger\Models\Thread;
55
use Cmgmyr\Messenger\Models\Message;
66
use Cmgmyr\Messenger\Models\Participant;
7+
use Illuminate\Database\Eloquent\ModelNotFoundException;
78
use Illuminate\Support\Facades\Auth;
9+
use Illuminate\Support\Facades\Input;
10+
use Illuminate\Support\Facades\Redirect;
11+
use Illuminate\Support\Facades\Session;
12+
use Illuminate\Support\Facades\View;
813

914
class MessagesController extends BaseController
1015
{
@@ -47,7 +52,13 @@ public function index()
4752
*/
4853
public function show($id)
4954
{
50-
$thread = Thread::findOrFail($id);
55+
try {
56+
$thread = Thread::findOrFail($id);
57+
} catch (ModelNotFoundException $e) {
58+
Session::flash('error_message', 'The thread with ID: ' . $id . ' was not found.');
59+
60+
return Redirect::to('messages');
61+
}
5162

5263
// show current user in list if not a current participant
5364
// $users = User::whereNotIn('id', $thread->participantsUserIds())->get();
@@ -122,7 +133,13 @@ public function store()
122133
*/
123134
public function update($id)
124135
{
125-
$thread = Thread::findOrFail($id);
136+
try {
137+
$thread = Thread::findOrFail($id);
138+
} catch (ModelNotFoundException $e) {
139+
Session::flash('error_message', 'The thread with ID: ' . $id . ' was not found.');
140+
141+
return Redirect::to('messages');
142+
}
126143

127144
$thread->activateAllParticipants();
128145

src/Cmgmyr/Messenger/examples/views/index.blade.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
@extends('layouts.master')
22

33
@section('content')
4+
@if (Session::has('error_message'))
5+
<div class="alert alert-danger" role="alert">
6+
{{Session::get('error_message')}}
7+
</div>
8+
@endif
49
@if($threads->count() > 0)
510
@foreach($threads as $thread)
611
<?php $class = $thread->isUnread($currentUserId) ? 'alert-info' : ''; ?>

0 commit comments

Comments
 (0)