dongqin5604 2017-10-02 18:57
浏览 100
已采纳

尝试和捕捉laravel它不适合我

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)

  • 写回答

1条回答 默认 最新

  • dreljie602951 2017-10-02 19:20
    关注

    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');
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭