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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧