HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.33
System: Linux li317-225.members.linode.com 3.10.0-1062.12.1.el7.x86_64 #1 SMP Tue Feb 4 23:02:59 UTC 2020 x86_64
User: apache (48)
PHP: 7.4.33
Disabled: NONE
Upload Files
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));
    }

}