i've a little problem, I'm trying to add the validations (Try and Catch) on my laravel project, the problem is that in some controllers works fine, but in a specific doesn't work, the validation is when try to load a page, and something went wrong, the aplication will redirect the user to another stable page with a message; here's my code:
public function info($id)
{
try {
$likes = $this->interactionAndUser($id)[0];
$dislikes = $this->interactionAndUser($id)[1];
$downloads = $this->interactionAndUser($id)[4];
$favorite = $this->interactionAndUser($id)[5];
$myLike= $this->interactionAndUser($id)[2];
$myDisLike = $this->interactionAndUser($id)[3];
$book = $this->interactionAndUser($id)[6];
$fileExistEpub = $this->interactionAndUser($id)[9];
$fileExistPdf = $this->interactionAndUser($id)[10];
$books = Book::find($id);
$forum = Forum::where('book_id', $id)->first();
$forumId = $forum->id;
$forumTheme = $forum->theme_id;
$forumHasTheme = Theme::where('id', $forumTheme)->first();
$comments = Comment::where(['forum_id' => $forumId, 'comment_id' => null])
->paginate(5);
if(Cache::has($id)==false) { // Si el ID tiene un valor falso o 0 para el cache, agregue 1
Cache::add($id, 'contador', 0.05); // Cada 0.05 segundos se contara una nueva visita por usuario, que recargue la pagina
$book->views+=1;
$book->save();
}
return view('books/info', compact('books', 'book', 'likes', 'dislikes', 'favorite', 'downloads', 'myLike', 'myDisLike', 'forum', 'forumHasTheme', 'comments', 'fileExistEpub', 'fileExistPdf'));
} catch (\Exception $e) {
return redirect('books')->with('errors', 'Ha ocurrido un errror, lo sentimos');
}
} -> This code work perfectly
As you can see, is a simple validation of the exception of Laravel, this works well, if something went wrong, the user will be redirect to another stable page, this piece of code is in a controller called BookController, the problem is with FrontController, and another function, when I try pass the validation, never redirect the user to a stable page; here my code
public function info_novelty($id)
{
try {
$novelty = Novelty::find($id);
return view('news.show', compact('novelty'));
} catch (\Exception $e) {
return redirect('noticias')->with('errors', 'Ha ocurrido un errror, lo sentimos');
}
} -> This validation doesn't work
I don't know why happens this, if someone can help me, I'll be really gratefull
And this is the error that I got it
Trying to get property of non-object (View: /home/vagrant/Code/Biblio/resources/views/news/show.blade.php)
You can use the findOrFail() method to ensure an exception is thrown if the resource is not found for the given id.
If no resource is found, the findOrFail()
method will throw a ModelNotFoundException
, so your code can look for it specifically:
try {
$novelty = Novelty::findOrFail($id);
return view('news.show', compact('novelty'));
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
// return more specific error message
} catch (\Exception $e) {
return redirect('noticias')->with('errors', 'Ha ocurrido un errror, lo sentimos');
}