1
0
moell 8 жил өмнө
parent
commit
27639c0d22

+ 4 - 1
app/Http/Controllers/HomeController.php

@@ -23,9 +23,12 @@ class HomeController extends Controller
     public function index()
     {
         $articles = $this->article
+            ->with([
+                'category'
+            ])
             ->orderBy('sort','desc')
             ->orderBy('id', 'desc')
-            ->paginate(15, ['id','title','desc','user_id','cate_id','read_count','created_at']);
+            ->paginate();
         return view('default.home', compact('articles'));
     }
 }

+ 5 - 4
app/Models/Article.php

@@ -42,19 +42,20 @@ class Article extends Model implements Transformable
     }
 
     /**
-     * @return \Illuminate\Database\Eloquent\Relations\HasOne
+     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
      */
     public function category()
     {
-        return $this->hasOne('App\Models\Category', 'id', 'cate_id');
+        return $this->belongsTo('App\Models\Category', 'cate_id');
     }
 
     /**
-     * @return \Illuminate\Database\Eloquent\Relations\HasOne
+     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
      */
     public function user()
     {
-        return $this->hasOne('App\Models\User', 'id', 'user_id');
+        return $this->belongsTo('App\Models\User', 'user_id');
     }
 
+
 }

+ 6 - 1
resources/views/default/article.blade.php

@@ -16,9 +16,14 @@
                         <i class="glyphicon glyphicon-calendar"></i>{{ date('Y-m-d', strtotime($article->created_at)) }}
                     </span>
                             &nbsp;
+                    @if($article->category)
                     <span>
-                        {!! $catePresenter->getIdLink($article->cate_id) !!}
+                        <i class="glyphicon glyphicon-th-list"></i>
+                        <a href="{{ route('category', ['id' => $article->cate_id]) }}" target="_blank">
+                            {{ $article->category->name }}
+                        </a>
                     </span>
+                    @endif
                     <span>
                         <i class="glyphicon glyphicon-eye-open"></i> {{ $article->read_count }} views
                     </span>