5
5
use Cms \Classes \Page ;
6
6
use Cms \Classes \ComponentBase ;
7
7
use RainLab \Blog \Models \Post as BlogPost ;
8
- use Illuminate \Database \Eloquent \ModelNotFoundException ;
9
8
10
9
class Post extends ComponentBase
11
10
{
@@ -71,6 +70,10 @@ public function onRun()
71
70
{
72
71
$ this ->categoryPage = $ this ->page ['categoryPage ' ] = $ this ->property ('categoryPage ' );
73
72
$ this ->post = $ this ->page ['post ' ] = $ this ->loadPost ();
73
+ if (!$ this ->post ) {
74
+ $ this ->setStatusCode (404 );
75
+ return $ this ->controller ->run ('404 ' );
76
+ }
74
77
}
75
78
76
79
public function onRender ()
@@ -85,26 +88,24 @@ protected function loadPost()
85
88
$ slug = $ this ->property ('slug ' );
86
89
87
90
$ post = new BlogPost ;
91
+ $ query = $ post ->query ();
88
92
89
- $ post = $ post ->isClassExtendedWith ('RainLab.Translate.Behaviors.TranslatableModel ' )
90
- ? $ post ->transWhere ('slug ' , $ slug )
91
- : $ post ->where ('slug ' , $ slug );
92
-
93
- if (!$ this ->checkEditor ()) {
94
- $ post = $ post ->isPublished ();
93
+ if ($ post ->isClassExtendedWith ('RainLab.Translate.Behaviors.TranslatableModel ' )) {
94
+ $ query ->transWhere ('slug ' , $ slug );
95
+ } else {
96
+ $ query ->where ('slug ' , $ slug );
95
97
}
96
98
97
- try {
98
- $ post = $ post ->firstOrFail ();
99
- } catch (ModelNotFoundException $ ex ) {
100
- $ this ->setStatusCode (404 );
101
- return $ this ->controller ->run ('404 ' );
99
+ if (!$ this ->checkEditor ()) {
100
+ $ query ->isPublished ();
102
101
}
102
+
103
+ $ post = $ query ->first ();
103
104
104
105
/*
105
106
* Add a "url" helper attribute for linking to each category
106
107
*/
107
- if ($ post && $ post ->categories ->count ()) {
108
+ if ($ post && $ post ->exists && $ post -> categories ->count ()) {
108
109
$ post ->categories ->each (function ($ category ) {
109
110
$ category ->setUrl ($ this ->categoryPage , $ this ->controller );
110
111
});
0 commit comments