How to Clear cache in Laravel

Many times you face an issue that any changes to the Laravel application are not reflecting on web interfaces. This occurred due to the application is being served by the cache. This tutorial will help you to clear cache in the Laravel application.

Clear Cache in Laravel (Terminal)

Log in to the system running your Laravel application and open a terminal. Then navigate to your Laravel application code. Here you can issue the commands to clear cache as followings:

1. Clear Application Cache

Run the following command to clear application cache of the Laravel application.

php artisan cache:clear

2. Clear route cache

To clear route cache of your Laravel application execute the following command from the shell.

php artisan route:cache

3. Clear config cache

You can use config:clear to clear the config cache of the Laravel application.

php artisan config:clear 

4. Clear compiled view files

Also, you may need to clear compiled view files of your Laravel application. To clear compiled view files run the following command from the terminal.

php artisan view:clear 

Clear Cache in Laravel (Browser)

Most of the shared hosting providers don’t provide SSH access to the systems. In that case, you can clear Laravel cache by calling URL in the browser. You can simply place the below code in your routes/web.php file of Laravel application. Then access this URL in the browser to clear the cache of Laravel application.

Route::get('/clear-cache', function() {
    Artisan::call('cache:clear');
    return "Cache is cleared";
});

how to check laravel version

Introduction

Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for your use while developing your application. It is driven by the powerful Symfony Console component.

Usage

Listing All Available Commands

To view a list of all available Artisan commands, you may use the list command:

php artisan list

Viewing The Help Screen For A Command

Every command also includes a “help” screen which displays and describes the command’s available arguments and options. To view a help screen, simply precede the name of the command with help:

php artisan help migrate

Specifying The Configuration Environment

You may specify the configuration environment that should be used while running a command using the --env switch:

php artisan migrate --env=local

Displaying Your Current Laravel Version

You may also view the current version of your Laravel installation using the --version option:

php artisan --version

www to https redirect laravel | laravel htaccess redirect to https

 What about just using .htaccess file to achieve https redirect? This should be placed in project root (not in public folder). Your server needs to be configured to point at project root directory.
<IfModule mod_rewrite.c>  
RewriteEngine On
 # Force SSL    
RewriteCond %{HTTPS} !=on    
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]    
# Remove public folder form URL    
RewriteRule ^(.*)$ public/$1 [L] 
</IfModule>
I use this for laravel 5.4 (latest version as of writing this answer) but it should continue to work for feature versions even if laravel change or removes some functionality. 

What is Eloquent ORM in Laravel?

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding “Model” which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

ORM: Object-relational Mapping (ORM) is a programming technique that help in converting data between incompatible type systems into object-oriented programming languages.

Laravel uses “Eloquent ORM” which allows you to work with your database objects and relationships using an eloquent and expressive syntax.

Eloquent is a new advance technique for query, using modal in Laravel. No need to write long queries, eloquent provides simple syntax to gain complex queries with in few seconds.

Below are Top 10 Key Features of Laravel Framework, that made it distant from others.

1. It’s Modularity

In Software Industry, Modularity refers to a degree in which a large system or application components/modules may be divided into smaller components and recombined.In Modularity, you can split your business logic into different parts which belong to single Entity.Laravel provides modular development approach in form of Packages that are pluggable.

2. Authentication

Authentication is one of the most important part of any web application, developers spent enormous time writing the authentication code again & again.Laravel 5 provide simple inbuilt authentication that can be easily integrated with your Laravel application by running few artisan commands.

3. Application Logic

In Laravel Application logic is written in Controllers or directly in routes.

4. Caching

Caching is used for storage of data on a temporary basis for a while and can be retrieved quickly.Using caching can reduce the load on the database server.Laravel provides an expressive, unified API to interact with the various caching backends.Laravel supports caching backends like Memcached and Redis that are easily configured with Laravel.

5. Method or Dependency Injection

Laravel’s service container is a powerful tool for managing class dependencies and performing dependency injection in the application. Dependency injection is a fancy phrase that essentially means this: class dependencies are “injected” into the class via the constructor or, in some cases, “setter” methods.(source https://laravel.com/docs/5.5/container)

6.Routing

In Laravel Routing is used for attaching Application URLs to a specific controller action or view.In Laravel 5.4 all routes are defined in your route files under routes folder. A basic route in Laravel takes an URL and a Closure function.you can read more about routing from https://laravel.com/docs/5.5/routing.

7.Automatic Pagination

Laravel has inbuilt paginations, that elimate the headache of writting code for paginating records

8.Template Engine

Blade is a simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. All Blade templates files should use the .blade.php extension.

9.Database Query Builder

Laravel’s database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works on all supported database systems. The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks. There is no need to clean strings being passed as bindings.

10.Eloquent ORM

In Laravel Eloquent is ORM that implements Active Records.Active Record Implementation is an architectural pattern found in software engineering that stores in-memory object data in relational databases.

Laravel Release Process

At Laracon 2013 in Washington D.C., Taylor announced the first official release cycle for the framework.

The way releases are set up is that a new version of Laravel is released every six months, one around June and another at the end of each year. Bug fixes are provided for six months and security fixes for one year.

By having an official release cycle, it allows us end users to plan around when we need to perform upgrades and also the dev team a way of knowing what is coming and when. Since this announcement, all new releases have followed this schedule.

With the release of Laravel 5.1, a new cycle was added to offer long-term support. It includes bug fixes for two years and security fixes for three.

This means 5.1 will be the only LTS version available until the two-year mark ends, June 2017. At that point, another LTS version will be announced. Here is an outline for the release schedule:

Proposed Release Schedule

5.1 LTS – Jun 9, 2015

Bug fixes until June 2017, and security fixes until June 2018.

Laravel 5.2 – Dec 21, 2015

General release of 5.2 which includes 6 months of bug fixes, 1 year of security.

Laravel 5.3 – Aug 23, 2016

6 months of bug fixes, 1 year of security.

Laravel 5.4 – January 24, 2017

6 months of bug fixes, 1 year of security. Here is a look at the new features of Laravel 5.4

Laravel 5.5 LTS – August 30, 2017

This is the current LTS release. At this point, Laravel 5.1 will no longer receive bug fixes but will continue getting security fixes until June 2018. Check out the what’s new in Laravel 5.5 series to see all the new features in this release.

Laravel 5.6 – February 7th, 2018

6 months of bug fixes, 1 year of security. what’s new in Laravel 5.6.

Laravel 5.7 – September 4th, 2018

6 months of bug fixes, 1 year of security. Check out the what’s new in Laravel 5.7series to see all the new features in this release.

History of Laravel Releases

VersionReleaseBug Fixes UntilSecurity Fixes Until
V1June 2011
V2September 2011
v3February 2012
v4May 2013
5.0Feb 4th, 2015Aug 4th, 2015Feb 4th, 2016
5.1 (LTS)Jun 9th, 2015Jun 9th, 2017Jun 9th, 2018
5.2Dec 21st, 2015Jun 21st, 2016Dec 21st, 2016
5.3Aug 23rd, 2016Feb 23rd, 2017Aug 23rd, 2017
5.4Jan 24th, 2017Jul 24th, 2017Jan 24th, 2018
5.5 (LTS)Aug 30th, 2017Aug 30th, 2019Aug 30th, 2020
5.6Feb 7th, 2018Aug 7th, 2018Feb 7th, 2019
5.7Sep 4, 2018Feb 4th, 2019Sep 4th, 2019

multiple database connection in laravel

multiple database connection in laravel

.env file

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database1 //database first
DB_DATABASE_OPENCART=database2    // database second
DB_USERNAME=root
DB_PASSWORD=aaaa@#2233

config/database

‘mysql’ => [
‘driver’ => ‘mysql’,
‘host’ => env(‘DB_HOST’, ‘localhost’),
‘port’ => env(‘DB_PORT’, ‘3306’),
‘database’ => env(‘DB_DATABASE’, ‘database1’),
‘username’ => env(‘DB_USERNAME’, ‘root’),
‘password’ => env(‘DB_PASSWORD’, ‘aaaa@#2233’),
‘charset’ => ‘utf8’,
‘collation’ => ‘utf8_unicode_ci’,
‘prefix’ => ”,
‘strict’ => false,
‘engine’ => null,
],
‘opencart’ => [
‘driver’ => ‘mysql’,
‘host’ => env(‘DB_HOST’, ‘localhost’),
‘port’ => env(‘DB_PORT’, ‘3306’),
‘database’ => env(‘DB_DATABASE_OPENCART’, ‘database2’),
‘username’ => env(‘DB_USERNAME’, ‘root’),
‘password’ => env(‘DB_PASSWORD’, ‘aaaa@#2233’),
‘charset’ => ‘utf8’,
‘collation’ => ‘utf8_unicode_ci’,
‘prefix’ => ”,
‘strict’ => false,
‘engine’ => null,
],

In Controller file

$db_ext = DB::connection(‘opencart’);
$countries = $db_ext->table(‘fit_cart’)->get();
print_r($countries);

INSTALL LARAVEL ON UBUNTU 17.04 / 17.10 WITH APACHE2, MARIADB AND PHP SUPPORT

INSTALL LARAVEL ON UBUNTU 17.04 / 17.10 WITH APACHE2, MARIADB AND PHP SUPPORT

Laravel is a flexible and lightweight open source PHP framework with Model-View Controller (MVC) design pattern. It can be a great alternative to CodeIgniter framework. Laravel is designed for ease of use to allow developers create great applications.

If you’re looking for a simple and straightforward PHP framework to design your next application, you’ll find Laravel to be useful. This brief tutorial is going to show students and new users how to install Laravel PHP framework on Ubuntu 17.04 / 17.10 with Apache2, MariaDB and PHP support.

To get started with installing Laravel, follow the steps below:

Step 1: Install Apache2

Laravel requires a webserver to function and the second most popular webserver in use today is Apache2. So, go and install Apache2 on Ubuntu by running the commands below:

sudo apt install apache2

Next, run the commands below to stop, start and enable Apache2 service to always start up with the server boots.

sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl enable apache2.service

Step 2: Install MariaDB

Laravel also requires a database server… and MariaDB database server is a great place to start. To install it run the commands below.

sudo apt-get install mariadb-server mariadb-client

After installing, the commands below can be used to stop, start and enable MariaDB service to always start up when the server boots.

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

After that, run the commands below to secure MariaDB server.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]:  Y
  • Reload privilege tables now? [Y/n]:  Y

Restart MariaDB server

sudo systemctl restart mariadb.service

Step 3: Install PHP And Related Modules

Laravel is based on PHP.. so you’ll need to install it. To install PHP and related modules run the commands below

sudo apt install php libapache2-mod-php php-common php-mbstring php-xmlrpc php-soap php-gd php-xml php-mysql php-cli php-mcrypt php-zip

After install PHP, run the commands below to open PHP-FPM default file.

sudo nano /etc/php/7.1/apache2/php.ini           # Ubuntu 17.10
sudo nano /etc/php/7.0/apache2/php.ini           # Ubuntu 17.04

Then make the change the following lines below in the file and save.

memory_limit = 256M
upload_max_filesize = 64M
cgi.fix_pathinfo=0

Step 4: Install Composer To Download Laravel

Run the commands below to install composer package and install.. you must have curl package installed for the commands to work.. if not, just run sudo apt install curl to install it…

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Change into Laravel directory and run the commands below to download and install Laravel for th e project you want to create… name the project whatever you want… for this post, we’re calling it MyProject..

cd /var/www/html
sudo composer create-project laravel/laravel MyProject --prefer-dist

After running the commands above, a new project directory will be created… Run the commands below to set the correct permissions for that directory..

sudo chown -R www-data:www-data /var/www/html/MyProject/
sudo chmod -R 755 /var/www/html/MyProject/

Step 5: Configure Apache2

Finally, configure Apahce2 site configuration file for Laravel. This file will control how users access Laravel content. Run the commands below to create a new configuration file called laravel.conf

sudo nano /etc/apache2/sites-available/laravel.conf

Then copy and paste the content below into the file and save it. Replace the highlighted line with your own domain name and directory root location.

<VirtualHost *:80>   
  ServerAdmin admin@example.com
     DocumentRoot /var/www/html/MyProject/public
     ServerName example.com

     <Directory /var/www/html/MyProject/public>
        Options +FollowSymlinks
        AllowOverride All
        Require all granted
     </Directory>

     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save the file and exit.

Step 6: Enable The Laravel And Rewrite Module

After configuring the VirtualHost above, enable it by running the commands below

sudo a2ensite laravel.conf
sudo a2enmod rewrite

Step 7: Restart Apache2

To load all the settings above, restart Apache2 by running the commands below.

sudo systemctl restart apache2.service



Then open your browser and browse to the server domain name. You should see Laravel page.

http://example.com

You should then see Laravel default home screen

how to install laravel

how to install laravel

Install Composer

Laravel utilizes Composer to manage its dependencies. First, download a copy of the composer.phar. Once you have the PHAR archive, you can either keep it in your local project directory or move to usr/local/bin to use it globally on your system. On Windows, you can use the Composer Windows installer.

Install Laravel

Via Laravel Installer

First, download the Laravel installer using Composer.

composer global require "laravel/installer=~1.1"

Make sure to place the ~/.composer/vendor/bin directory in your PATH so the laravelexecutable is found when you run the laravel command in your terminal.

Once installed, the simple laravel new command will create a fresh Laravel installation in the directory you specify. For instance, laravel new blog would create a directory named blog containing a fresh Laravel installation with all dependencies installed. This method of installation is much faster than installing via Composer.

Via Composer Create-Project

You may also install Laravel by issuing the Composer create-project command in your terminal:

composer create-project laravel/laravel {directory} 4.2 --prefer-dist

Via Download

Once Composer is installed, download the 4.2 version of the Laravel framework and extract its contents into a directory on your server. Next, in the root of your Laravel application, run the php composer.phar install (or composer install) command to install all of the framework’s dependencies. This process requires Git to be installed on the server to successfully complete the installation.

If you want to update the Laravel framework, you may issue the php composer.phar updatecommand.

Server Requirements

The Laravel framework has a few system requirements:

  • PHP >= 5.4
  • MCrypt PHP Extension

As of PHP 5.5, some OS distributions may require you to manually install the PHP JSON extension. When using Ubuntu, this can be done via apt-get install php5-json.

Configuration

The first thing you should do after installing Laravel is set your application key to a random string. If you installed Laravel via Composer, this key has probably already been set for you by the key:generate command. Typically, this string should be 32 characters long. The key can be set in the app.php configuration file. If the application key is not set, your user sessions and other encrypted data will not be secure.

Laravel needs almost no other configuration out of the box. You are free to get started developing! However, you may wish to review the app/config/app.php file and its documentation. It contains several options such as timezone and locale that you may wish to change according to your application.

Once Laravel is installed, you should also configure your local environment. This will allow you to receive detailed error messages when developing on your local machine. By default, detailed error reporting is disabled in your production configuration file.

Note: You should never have app.debug set to true for a production application. Never, ever do it.

Permissions

Laravel may require one set of permissions to be configured: folders within app/storagerequire write access by the web server.

Paths

Several of the framework directory paths are configurable. To change the location of these directories, check out the bootstrap/paths.php file.

Pretty URLs

Apache

The framework ships with a public/.htaccess file that is used to allow URLs without index.php. If you use Apache to serve your Laravel application, be sure to enable the mod_rewrite module.

If the .htaccess file that ships with Laravel does not work with your Apache installation, try this one:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Nginx

On Nginx, the following directive in your site configuration will allow “pretty” URLs:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}