The queries on HomeController file slows down the site. It takes 20 seconds for the page to load fully. (Page size is 3.9 Mb only, and CPU load goes up to 80% every time when i load the page). I am told to use Query Builder which is faster than Elequant and join queries to send them as one query. I find this too difficult. Where can i see some examples for this ?
HomeController
public function index()
{
$sliders = Post::where('post_type','slider')
->with('FeaturedImage','PostField')
->orderBy('created_at', 'desc')
->limit(4)
->get();
$page1 = Post::where([
['post_type','=','custom_page'],
['slug','=','page1'],
])
->with('FeaturedImage','PostField')
->latest()
->first();
$page2 = Post::where([
['post_type','=','custom_page'],
['slug','=','page2'],
])
->with('FeaturedImage','PostField')
->latest()
->first();
$page3 = Post::where([
['post_type','=','custom_page'],
['slug','=','page-3'],
])
->with('FeaturedImage','PostField')
->latest()
->first();
$compacts = array(
'sliders',
'page1',
'page2',
'page3',
);
return view('site.home')->with(compact($compacts));
}
edit: Post Migration
public function up()
{
// Create table for storing roles
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('author_id');
$table->integer('category_id')->nullable();
$table->string('title');
$table->text('excerpt')->nullable();
$table->text('body')->nullable();
$table->string('slug')->nullable();//unique()
$table->string('post_type')->default('post');
$table->enum('status', ['PUBLISHED', 'DRAFT', 'PENDING'])->default('DRAFT');
$table->timestamps();
});
}