简体   繁体   中英

Laravel HomePage is loading too slow (30s)

I'm doing social website. But i have a problem about load homepage so slow (30-40s). Can i fix something? Help me, Please. This is HomeController

public function index(Request $request){
        $category_post = Category::orderBy('category_id', 'DESC')->get();
        $post2 = Post::orderBy('tbl_post.created_at','DESC')->paginate(5);
        
        return view('student.page.home')->with(compact('category_post','post2'));
}

1 part of my foreach

@foreach($post2 as $key => $post_info)
<p style="font-size: 20px;" class="widget-box-status-text">{{$post_info->post_title}}</p>
<br>
<p style="white-space: pre-line;" class="read-more widget-box-status-text">{{$post_info->post_content}}</p>
<div class="tag-list">
 <a class="tag-item secondary" style="font-size: 16px" href="{{url('/quest-category/'.$post_info->category_id)}}">{{$post_info->category->category_name}}</a>
</div>
@endforeach

Post Model

class Post extends Model
{
    public $timestamps = FALSE;
    protected $fillable = [
        'post_student_name', 'post_student_email', 'post_title', 'category_id', 'post_content', 'created_at'
    ];
    protected $primaryKey = 'post_id';
    protected $table = 'tbl_post';

    public function student(){
        return $this->belongsTo('App\Models\Student','student_id');
    }
    public function category(){
        return $this->belongsTo('App\Models\Category','category_id');
    }
}

Here you can optimize your eloquent query using Eager Loading:

$post2 = Post::with('category')->orderBy('tbl_post.created_at','DESC')->paginate(5);

It also depends on many other things, for example, Network connection, Cache, CSS, and JS file, loaded asset, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM