File: /var/www/hobbyistgarage/app/Http/Controllers/BlogController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use App\Models\Article;
class BlogController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
$articles = Article::where('published', true)->paginate(20);
return view('newsandreviews.index')->with(array("articles" => $articles));
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
//$article = Article::where('short_url', $id)->first();
$article = Cache::remember('article-'.$id, 1440, function() use ($id) {
return Article::where('short_url', $id)->first();
});
$tag_ids = $article->tags()->pluck('tag_id');
$populars = Article::where('published',true)->where('id',"!=",$article->id)->orderBy('likes', 'desc')->limit(5)->get();
$editors = Article::where('published',true)->where('id',"!=",$article->id)->where('editor_pick',true)->orderBy('likes', 'desc')->limit(5)->get();
//$comments = Comment::orderBy('created_at','desc')->limit(5)->get();
$trending = Article::where('published',true)->where('id',"!=",$article->id)->orderByRaw('LOG10(ABS(likes - dislike) + 1) * SIGN(likes - dislike) + (UNIX_TIMESTAMP(published_at) / 300000) DESC')->limit(5)->get();
$related_articles = Cache::remember('related-article-'.$id, 1440, function() use ($id, $tag_ids, $article) {
return Article::where('published', true)->where('id',"!=",$article->id)->whereHas('tags', function($q) use ($tag_ids) {
$q->whereIn('tag_id', $tag_ids);
})
->orderBy('published_at')
->take(3)
->get();
});
//$related_articles
$article->increment('views');
return view('newsandreviews.show')->with(array("article" => $article, "related_articles" => $related_articles, "populars" => $populars, "editors" => $editors, "trending" => $trending));
}
}