Performance Boost using Model Caching in Laravel 6

Performance Boost using Model Caching in Laravel 6

This Laravel 6 tutorial explains how to use model caching to boost our Laravel apps' performance. Performance Boost using model caching in Laravel 6. How to improve performance in Laravel 6 using Laravel model caching. We can Performance Boost using eloquent model caching via genealabs/laravel-model-caching git composer package in Laravel 6.

In this tutorial, we will discuss how to improve performance in Laravel 6 using Laravel model caching. We can Performance Boost using eloquent model caching via genealabs/laravel-model-caching git composer package in Laravel 6.

As we know, website performance is very important for site owner. If your website working smooth then traffic will be more and if you used eloquent cache then it's no longer load on server too. so in this post i will show you how to make mode caching and make performance boost you laravel application.

there are several ways to improve the performance of laravel website. As you know we almost looking to cache using htaccess and cache images, css, js and html file.

But if you have to fetch many records from a database and from many tables. so when you open link then more than one query always run on your web page. it takes time to fetch data from the database because it same process always when you load page, so basically it loads every time. At a long time when you have more data and mode visitors comes in our website then your server will be broken, but if you use laravel cache then it can save your server and improve page performance. Here we will use genealabs/laravel-model-caching package for model caching.

Laravel cache is very easy and simple way to use. Using Cache you will optimize your website page speed. So let's just follow bellow step and see how it works. We will use barryvdh/laravel-debugbar package also, this package will help to debug how many query fire on your page so you can see that. So just follow bellow step.

Step 1: Install Laravel 6 App

we are going to from scratch so, we need to get fresh Laravel 6 application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog
Step 2: Installation Of barryvdh/laravel-debugbar Package

Now we will install barryvdh/laravel-debugbar composer package using by following command in our laravel 6 application. So let's run bellow command.

composer require barryvdh/laravel-debugbar --dev

Ok, after install package successfully, we will add service provider in app.php configuration file. So let's add as bellow:

config/app.php

<?php


return [

	....

	'providers' => [

		....

		Barryvdh\Debugbar\ServiceProvider::class,

	],

	'aliases' => [

		....

		'Debugbar' => Barryvdh\Debugbar\Facade::class,

	]

]

Then you can publish configuration files by following command:

php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"
Step 3: Installation Of genealabs/laravel-model-caching Package

Now we will install genealabs/laravel-model-caching composer package using by following command in our laravel 6 application. So let's run bellow command.

composer require genealabs/laravel-model-caching
Step 4: Update User Model

In this step, we will use GeneaLabs package class in User model So you have to update user model like as bellow:

app/User.php

<?php


namespace App;


use Illuminate\Notifications\Notifiable;

use Illuminate\Foundation\Auth\User as Authenticatable;

use GeneaLabs\LaravelModelCaching\Traits\Cachable;


class User extends Authenticatable

{

    use Cachable;

    use Notifiable;


    /**

     * The attributes that are mass assignable.

     *

     * @var array

     */

    protected $fillable = [

        'name', 'email', 'password',

    ];


    /**

     * The attributes that should be hidden for arrays.

     *

     * @var array

     */

    protected $hidden = [

        'password', 'remember_token',

    ];

}
Step 5: Create Dummy Users

Here we will create some dummy users records using factory. so you can run bellow command to create dummy users in your database.

php artisan tinker

factory(App\User::class, 100)->create();
Step 6: Create Route

now will create one route for display users in view. so let's create one route as bellow listed:

routes/web.php

Route::get('users', '[email protected]');
Step 7: Create Controller

we need to create new controller as UserController and then we will create index() on controller. we will get all users using User model and print in view:

app/Http/Controllers/UserController.php

<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;

use App\User;


class UserController extends Controller

{

	

	/**

     * The application's index

     *

     * @var array

     */

    public function index()

    {

    	$users = User::where('id', 1)->get();

    	$users = User::where('id', 5)->get();

    	$users = User::get();


    	return view('users',compact('users'));

    }

}
Step 8: Create Blade File

now at last we will create users.blade.php file and write code of display users lists. so let's create blade file:

resources/views/users.blade.php

<!DOCTYPE html>

<html>

<head>

	<title>User List</title>

	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous">

</head>

<body>


<div class="container">

	<h1>User List- ItSolutionStuff.com</h1>

	<table class="table table-bordered">

		<tr>

			<th>Id</th>

			<th>name</th>

			<th>email</th>

		</tr>

		@foreach($users as $user)

		<tr>

			<td>{{ $user->id }}</td>

			<td>{{ $user->name }}</td>

			<td>{{ $user->email }}</td>

		</tr>

		@endforeach

	</table>

</div>


</body>

</html>

Now you are ready to run project, so let's check.

When you open first time page then it will load three queries as bellow:

Then again refresh and check, no longer query fired on server:

You can cache clear and check again:

php artisan cache:clear

I hope it can help you....

Clear Cache in Laravel 6.8 App using Artisan Command Interface (CLI)

Clear Cache in Laravel 6.8 App using Artisan Command Interface (CLI)

In Laravel 6 tutorial, we learn how to use PHP artisan command interface (CLI) to clear the cache from Laravel 6.8 application. How To Clear Cache in Laravel 6.8 Application using Artisan Command Line Interface (CLI)? How to clear route cache using php artisan command? How to easily clear cache in Laravel application? How to clear config cache in PHP Laravel via artisan command? How to clear Laravel view cache? How to Reoptimized class in Laravel via artisan CLI?

Today in this tutorial, we are going to learn how to clear route cache, laravel application cache, config cache, view cache and reoptimized class in a Laravel 6.8 application using artisan command-line interface.

I’m pretty sure many of you may have found yourself gotten into the situation where you do not see changes in the view after making the changes in the app.

Laravel application serves the cached data so caching problem occurs due to the robust cache mechanism of Laravel.

But, if you are still facing this issue, then you do not have to worry further. Let me do the honour of introducing you some of the best artisan commands to remove the cache from your Laravel app via PHP artisan command line interface.

Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application.

Table of Contents

  • Clear Route Cache in Laravel
  • Clear Laravel Application Cache
  • Clear Config Cache via PHP Artisan
  • Clear Laravel View Cache
  • Reoptimized Class
Clear Route Cache in Laravel

Laravel caching system also takes routes in consideration, to remove route cache in Laravel use the given below command:

php artisan route:cache
Clear Application Cache in Laravel

Run the following command to clear application cache:

php artisan cache:clear
Clear Config Cache in Laravel

Run the following command to clear config cache:

php artisan config:cache
Clear View Cache in Laravel

Run the following command to clean your view cache:

php artisan view:clear
Reoptimize Class

Run the below command to reoptimize the class loader:

php artisan optimize

Conclusion

We have completed this Laravel 6 tutorial, In this tutorial we learned how to use php artisan command to clear the cache from your Laravel application. We have answered the following questions in this article.

  • How to clear route cache using php artisan command?
  • How to easily clear cache in Laravel application?
  • How to clear config cache in PHP Laravel via artisan command?
  • How to clear Laravel view cache?
  • How to Reoptimized class in Laravel via artisan CLI?

Now, it’s your time to let me know what do you think about this laravel 6 article. Go forth and try these super awesome artisan commands and let me know how these commands are helping you.

Get Weather Data with Laravel Weather

Get Weather Data with Laravel Weather

Get Weather Data with Laravel Weather. Laravel Weather is a good package which we can use to get weather data. It's a wrapper around Open Weather Map API (Current weather). A wrapper around Open Weather Map API (Current weather)

🌤️ A wrapper around Open Weather Map API (Current weather)

Installation

You can install the package via composer:

source-shell
composer require gnahotelsolutions/laravel-weather
Usage
text-html-php
$weather = new Weather();

// Checking weather by city name
$currentWeatherInGirona = $weather->get('girona,es');

// You can use the city id, this will get you unambiguous results
$currentWeatherInGirona = $weather->find('3121456');

Units

By default the package uses metric for Celsius temperature results, this can be modified using the configuration file or on the fly:

text-html-php
$weather = new Weather();

$currentWeatherInGirona = $weather->inUnits('imperial')->get('girona,es');

Language

By default the package uses es for the description translation, this can be modified using the configuration file or on the fly:

text-html-php
$weather = new Weather();

$currentWeatherInGirona = $weather->inLanguage('en')->get('girona');

Guzzle Client Instance

If you need to use another instance of Guzzle, to modify headers for example:

text-html-php
$weather = new Weather();

$guzzle = $this->getSpecialGuzzleClient();

$currentWeatherInGirona = $weather->using($guzzle)->get('girona');

Testing

source-shell
composer test

How to Use Sweet Alert in Laravel?

How to Use Sweet Alert in Laravel?

This Laravel tutorial explains how to use Sweet Alert in Laravel. Laravel SweetAlert is a Laravel package by Rashid Ali that integrates the popularJavaScript SweetAlert 2 library with Laravel. Sweet Alert is a Laravel wrapper around SweetAlert JavaScript library for showing alert or notification message.

This Laravel tutorial explains how to use Sweet Alert in Laravel. Laravel SweetAlert is a Laravel package by Rashid Ali that integrates the popularJavaScript SweetAlert 2 library with Laravel. Sweet Alert is a Laravel wrapper around SweetAlert JavaScript library for showing alert or notification message.

This is a great package created by rashid, and this laravel package is very famous because it has tonnes of features you can choose from.