Category Archives: Laravel

Laravel Tutorials

laravel Order By Example Tutorial

In this laravel Order By Example Tutorial, I’ll show you how to use order by with eloquent queries in laravel. In laravel tutorial you will learn how to use laravel order by with relation, date desc, desc limit, asc, all(), random, created_at, raw etc. I’ll also show you how to use multiple order by in one query.

Laravel OrderBy Example

In this tutorial, you will learn the following laravel orderBy with queries:

  • Laravel OrderBy
  • Laravel OrderBy Multiple
  • Laravel Order By Date Desc
  • Laravel Orderby Belongs to Relationship
  • Laravel Orderby with Limit

Simple Laravel OrderBy

Simple use of laravel orderBy is as following:

Laravel OrderBy Multiple

In laravel eloquent query multiple orderBy can be applied as following:

Laravel Order By Date Desc

Laravel orderBy date can used as following:

Laravel Orderby In Belongs to Relationship

In Laravel orderBy with relationship query can be applied as following:

Laravel Orderby with Limit

In laravel eloquent query orderBy with limit can used as following:

Laravel Disable CSRF Token Protection on Routes Example

In this Laravel disable CSRF token protection example tutorial, I’ll show you how to disable CSRF token protection on all routes and specific routes in laravel application.

Laravel Disable CSRF Token Protection on Routes Example

Working with laravel apps you commonly face problems like laravel csrf token mismatch, laravel csrf token expiration time, csrf token mismatch laravel ajax, and romove csrf token in laravel form. In this tutorial I will guide you how to remove csrf protection on all routes or specific routes in laravel apps.

Laravel Disable CSRF Token Protection

In this article you will learn to disable CSRF token protection any specific route  or all routes:

Laravel Disable CSRF Protection All Routes

To disable CSRF protection for all routes. Go to app/HTTP/ directory and Open Kernal.php file. Then remove or comment out this \App\Http\Middleware\VerifyCsrfToken::class line in app\Http\Kernel.php as following:

App\Http\Kernel.php

Laravel disable CSRF token protection for specific routes

In laravel, to disable csrf protection for specific route just follow steps below. For example you have following routes in your laravel application in that you want to disable CSRF protection:

routes\web.php

To disable csrf token for specified routes in your laravel application. Go to app\Http\Middleware directory and open VerifyCsrfToken.php file. Then specify the routes for which you want to disable csrf token as following:

App\Http\Middleware\VerifyCsrfToken.php

Laravel 7 Redirect to Previous Page After Login Example

In this laravel tutorial, I’ll show you how to redirect back to previous page after login in laravel project. In this tutorial you will learn to redirect user back to the previous page or url after login in laravel.

Laravel 7 Redirect to Previous Page After Login Example

In this article I’ll you show you two different methods to redirect user back to previous page after login.

First Method : How to redirect user Previous Page After Login

Open your loginContorller.php file then add this showLoginForm() method as following:

In this function we will be setting the “url.intended” session variable. Laravel uses this variable to check for the page to which user will be redirected after login.

Second Method : Laravel redirect user Previous Page After Login

In this method, you have to put the following code in LoginController’s __construct()method as follow:

As follow:

Laravel 7 Passport Refresh Token Example

In Laravel creates rest API with passport refresh token example tutorial, I’ll show you how to create restful api with laravel passport refresh token example.

In this tutorial you will learn step by step how to create rest API with passport refresh token in laravel. You will also learn how to set passport token expired time in this example and also get access token in laravel apps.

Laravel Create REST API with Passport Refresh Token Example Tutorial

In this step by step tutorial you will be able to create laravel api authentication and token with passport refresh token laravel apps:

  • Download Laravel New App
  • Add Database Detail
  • Install Passport Packages in Laravel
  • Run Migration and Install Passport Auth
  • Configuration Passport Package
  • Create APIs Route
  • Create Controller & Methods
  • Now Test Laravel REST API in Postman

Install Laravel Application

First of all we need to create a fresh laravel project, download and install Laravel using the below command

Database Configuration

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Install Passport Package In Laravel

In this step, you need switch to project directory and the following commands:

Once you have installed laravel passport, register providers. Open config/app.php. So update the following code into config/app.php file:

config/app.php

go to the app/providers/AppServiceProvider.php and put the two line of code inside a boot method :

Run Migration and Install Passport Auth

Now, run the following migration command.

Next, you need to generate a passport encryption key. So run the following command.

Configuration Passport Package

Now, we need to configure passport package. Go to App/User.php file and update the following code into your App/User.php file as following:

App/User.php

Now you need to register passport package in AuthServiceProvider.php So go to App/Providers/ and open AuthServiceProvider.php. Then update the file as following:

config/auth.php

Now, go to config/auth.php and Change the API driver to the session to passport. Put this code

in API  as following:

Create APIs Route

Now, you need to create rest API routes. Go to routes/api.php put the following routes into api.php file:

routes/api.php

Create Controller & Methods

Now, run the following command to create a controller:

After that, update the following methods into AuthController controller as following:

In the above controller, getTokenAndRefreshToken() method will generate passport token and refresh token for your laravel apps. Now, Open your terminal and run the following command to start the development server:

Now Test Laravel REST API in Postman

Now, we are good to teat API in postman

Laravel 7 Download File From Public Storage Folder

In this laravel download file from public storage folder example, I’ll show you how to download or display files from public storage folder in laravel application. In this tutorial you will learn how to download or display files from public storage folder in laravel.

Download Files From Public Storage Folder In Laravel

In this step by step tutorial I’ll guide you through the step to download files from public stroage folder. And as well as display files on laravel blade views:

Routes

In this step, you need to add the following routes on web.php file. Go to routes directory and open web.php file then update the routes as following:

web.php

Create Controller File

Now, go to app/controllers and create a controller file named FileController.php. Then update put the following methods in it:

The above code will download files from public storage by giving the file name and return a response with correct content type. To display files on blade views, so you have to update the following methods into your controller file:

The above lined of code fetches the image files from the public storage folder and extract the name of these files and you pass them to your view.

Create Blade View

Now, go to resources\view directory and create a new blade file named show.blade.php. Then put the following code in it:

Note that, if you are getting the following errors in laravel apps, when you are working with laravel files or storage:

1: “class ‘app\http\controllers\file’ not found”.

Import File in your controller file as follow:

2: “class ‘app\http\controllers\response’ not found”.

Import Response in your controller file as follow:

3: “class ‘app\http\controllers\storage’ not found”.

Import Storage in your controller file as follow:

Laravel 7 Delete File from Public Storage Folder

In this laravel delete files from public storage folder example tutorial, I’ll show you how to delete files from public storage folder. In this tutorial you learn to delete files from public storage folder.

Laravel 7 Delete File from Public Storage Folder

In this step by step tutorial I’ll guide through the step to delete files from public storage folder. Before you delete files from public storage folder, you need to check files exist in public storage folder or not.

In this how to delete files from public storage folder tutorial, I’ll show you following 3 different ways to delete files from the public storage folder:

Using File System

Using Storage System

Using Core PHP Functions

if you getting this error “class ‘app\http\controllers\file’ not found”. You can use file system as follow in your controller file:

import File so try

use File;

How to Deploy Laravel Project on Linux Server

In this Laravel deploy project on linux ubuntu server tutorial. II’ll show you how to deploy laravel project app on linux ubuntu server.

In this tutorial I’ll demonstrate you two different methods to deploy laravel project on linux server. The first method is deploy laravel project using git repo. And second method is to create a new Laravel project inside our project directory. And at the end of these solutions, adjust vertual host file.

Deploy Laravel Project App with Apache on Linux Ubuntu Server

In this tutorial we will use two different methods to deploy laravel project on linux ubuntu server.

first of all, we are required to install all the required dependencies on the linux ubuntu server. So use the following command to install all dependencies on linux ubuntu server:

Above commands will have installed composer on the linux server because install Laravel’s dependencies using composer update or composer install command.

Deploy Laravel Project Using Clone GIT Repository

To deploy Laravel from it’s official GitHub repository. And deploy laravel inside the default document root of Apache webserver. That means, go to /var/www/html and run the git clone command, as follow:

Deploy a New Laravel Project

To deploy a fresh Laravel project on your linux server. Now, you can use the composer and run the following command on your terminal as follow:

Now, you need to update .env file and generate an encryption key. So use the following command for that:

The above commands will copy the file from .env.example to .env and generate an encryption key.

Setup VertualHost On Linux Server

Now, you need to adjust vertualhost in your linux server. To open your virtual host file in edit mode, execute the following command.

Do not forget to replace 000-default.conf with the name of your virtual host file if you are using multiple virtual hosts on your server. Now, add /public at the end of the document root so that Apache will route all the traffic inside the /public route that holds the main index.php file of our project. For example, Here is the updated default virtual host configuration file without comments.

Once you done with updating virtual host file, press CTRL+X followed by Y followed by the Enter key to save the updated virtual host file.

Finally, we are good to restart the Apache server to apply the changes. Run the following command to restart the Apache server.

Now, your apache server successfully restarts, now you can  access your Laravel project in the browser.

Laravel Eloquent insertOrIgnore Example

In this Laravel insertOrIgnore() eloquent method example tutorial, I’ll show you how to use insertorignore query in laravel. In this tutorial you will learn how to implement and use Laravel insertOrIgnore() eloquent method . In this article I’ll share different use case of Laravel insertOrIgnore() eloquent method with the help of examples.

Laravel Eloquent insertOrIgnore Example

In this step by step tutorial I’ll guide you through use of Eloquent insertOrIgnore method. Support you want to insert bulk amount of records in DB table. But you want to skip the records that already exist in the table because you don’t want to have duplicate entry. In this case insertOrIgnore() method is helpful. Using insertOrIgnore() method while inserting records in laravel will ignore the records already exists. Insert records using insertOrIgnore() method in laravel can be done in following way:

Laravel InsertOrIgnore With Eloquent

In this example query we will learn to insert records using InsertOrIgnore() method in laravel with eloquent model.

Example:-

Laravel insertOrIgnore method ignore the duplicate record while inserting records into the database.

Laravel InsertOrIgnore Query With Eloquent Model

In this example query we will learn to insert records using InsertOrIgnore() query in laravel with eloquent model.

How to Get Random Records in Laravel

In this Laravel get a random record example tutorial, I’ll show you how to get random records from DB in laravel using inRandomOrder() method. In this article you will learn to get random records from DB in laravel.

How to Get Random Records in Laravel

While developing an laravel application you come to situations where you want to get random posts/data from the database in laravel. You may also want to display this random data anywhere on your laravel blog. In laravel this can be done using inRandomOrder() method to get random records in laravel. In this article I’ll guide you through step by step how to get random records from the database in laravel using inRandomOrder() method. You can also get random records from collection in laravel.

In this article I’ll share two different method to get random records from the database in laravel.

Laravel Retrieve Random Records From DB with Eloquent

In this method we will fetch the 5 random posts from DB table using eloquent in laravel:

Laravel Retrieve Random Records From DB using Model

In this method we will fetch the 5 random posts from DB table using laravel model in laravel:

Laravel 7 Left Join Example Tutorial

In this Laravel 7 left join example tutorial I’ll show you how to use left join in laravel. In this article you will also learn to implement and use laravel left join query. In this tutorial I’ll share different use case of the laravel left join eloquent query. In this Laravel 7 left join example tutorial you will learn about laravel left join syntax and use. We will also share example of laravel left join with multple where condition.

Laravel 7 Left Join Example Tutorial

Laravel left join query returns all rows from the left table, even if there is no matches in the right table. When you are using laravel left join eloquent with a query builder laravel left join fetch data from the left table. Even if there is no match in the right table.

Let suppose we have 2 tables posts and authors table. To illustrate left join get the data from the authors table. And left join posts with authors table.

Example 1: Laravel Left Join Query

Suppose we want to get data using laravel left join with authors and posts table, you can use left join query in laravel eloquent model as following:

Example:-

The above given leftJoin query you will get the following SQL query:

Example 2: Laravel Left Join with Multiple Condition

Suppose now you want to get data using laravel left join with multiple where conditions on authors and posts table, you can use left join as following:

The above given leftJoin query you will get the following SQL query:

Laravel 7 Right Join Example Tutorial

In this Laravel 7 right join example tutorial I’ll show you how to use right join in laravel. In this article you will also learn to implement and use laravel right join query. In this tutorial I’ll share different use case of the laravel right join eloquent query. In this Laravel 7 right join example tutorial you will learn about laravel right join syntax and use. We will also share example of laravel right join with multple where condition.

Laravel 7 right Join Example Tutorial

Laravel right join query returns all rows from the right table, even if there is no matches in the left table. When you are using laravel right join eloquent with a query builder laravel right join fetch data from the right table. Even if there is no match in the left table.

Let suppose you have 2 tables users and city table. Now you get the data from the users table with right join with city table.The right join query returns all rows in the city table and all matching rows found in the users table.

Example 1: Laravel Right Join Query

Suppose we want to get data using laravel left join with authors and posts table, you can use left join query in laravel eloquent model as following:

Example:-

Suppose we want to get data using laravel right join from users and city table.  then you can use rightjoin as following:

The above given rightJoin query you will get the following SQL query:

Example 2: Laravel Right Join with Multiple Condition

Suppose now you want to get data using laravel right join with multiple where conditions on users and city table., you can use rightjoin as following:

The above given rightJoin query you will get the following SQL query:

Laravel Eloquent Join 2 Tables Example

In this Laravel eloquent join 2, 3, or multiple tables example I’ll show you how to use laravel eloquent join with 2 or multiple tables for fetching data from multiple tables. In this laravel join tutorial you will also learn how to use laravel eloquent join() with multiple where conditions.

Laravel Eloquent Join

In laravel model when you want to fetch data from multiple table with associated relationship In that case you can fetch data using laravel joins. In laravel as per the need we have to use left join(), right join(), cross join() or to join two or multiple table.

Laravel Eloquent Join() with 2 Tables

Let suppose you want  to fetch data using laravel eloquent join(), from 2 table, then you can do it as following:

The above given join query you will get the following SQL query:

Laravel Eloquent Join() with 3 Tables

Let suppose you want  to fetch data using laravel eloquent join(), from 2 table, then you can do it as following:

The above given join query you will get the following SQL query:

Laravel Eloquent Join() with Multiple Conditions

Suppose now you want to get data using laravel eloquent join with multiple where conditions, you can use laravel eloquent join as following:

The above given join query you will get the following SQL query: