Category Archives: Laravel

Laravel Tutorials

Laravel Query Scope Example Tutorial

In this Laravel Query Scope Example Tutorial I will show you how to use query scopes in laravel eloquent model. In this tutorial you will learn to implement and use query scopes in laravel eloquent model. I will Also show you how to create and use dynamic query scope in laravel applications.

Laravel Query Scope Example Tutorial

In this step by step guide I will demonstrate you to create and use dynamic query scope in laravel applications.

Create Basic Scope in Model

Here we will first create a basic scope. Go to app/Post.php and create a scope here:

Use Basic Query Scope on Controller:

We can use basic query scope as following:

Create Dynamic Scope in Model

Now we are creating a dynamic query scope in laravel model as following:

Use Dynamic Scope Query On Controller

We can use dynamic scope in laravel as following:

Apply Scope to Relationship Laravel

Here, you will learn how to use query scopes with laravel relationship. Go to your App/Category.php and create a relationship between Categories and Posts Tables:

Go to your App/Post.php and create a relationship between Posts and Categories Tables:

Use query scope with relationship

Here we can use query scope with relationship as following:

Laravel 7 Google Autocomplete Address Example Tutorial

In this Laravel 7 Google Autocomplete Address Example Tutorial, I’ll show you how to integrate Address or location Autocomplete using Google Autocomplete in laravel 7. In this tutorial you will learn to implement google autocomplete in laravel 7.

Google Autocomplete

Google autocomplete widget is used to display suggestions for places, address and location as soon as the user starts typing in the given input box.

Laravel 7 Google Autocomplete Address Example Tutorial

In this laravel 7 google autocomplete tutorial I’ll demonstrate you step by step process to integrate google autocomplete in laravel 7 project. Pleasae follow the instructions given below:

  • Install Laravel Application
  • Get Api Key From Google
  • Create Route
  • Generate Controller by Command
  • Create Blade View
  • Run Development Server
  • Conclusion

Step 1: Install Laravel Application

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

Step 2: Get Api Key From Google

Now, we need to get Google API key to enable the communication between client and Google server. Now follow the instructions given below to get Google API Key:

  • Visit Google Cloud Platform.
  • Click on the project dropdown at the top to create the project.
  • Click APIs & Services > Credentials.
  • Now, click on Create Credentials > API key.
  • Copy google API and store in some text file.
  • Now we need to enable few services, so click on Credentials > “Enable APIs and Services”, additionally enable “Maps JavaScript API” and “Places API” services.

Step 3: Create Route

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Step 4: Generate Controller by Command

Now, lets create a controller named GoogleController using command given below –

After successfully create controller go to app/controllers/GoogleController.php and put the following code :

Step 5: Create Blade view

Now, we will create a blade view file. Go to app/resources/views and create one file name auto-complete.blade.php :

Add Autocomplete Script

Step 6: Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

Laravel 7/6 Ajax Multiple Image Upload with Preview

In this Laravel Multiple Image Upload with Preview, I’ll show you how to upload image along with preview before upload in laravel. In this laravel multiple image upload example, I’ll show you how to validate upload image into folder and then save it into database. In this tutorial before saving image into database we will show preview of the images and then save it into directory. Before uploading the image we will display preview of the image. After successfully image upload into the database and folder we will display success message on the screen.

Laravel 7/6 Ajax Multiple Image Upload with Preview

  • Install Laravel Fresh Setup
  • Setup Database Credentials
  • Create Route
  • Generate Controller By Command
  • Create the blade view
  • Start Development Server

1: Install Laravel Fresh Setup

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

2: Setup Database Credentials

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.

3: Create Route

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

4: Generate Controller by Command

Now, lets create a controller named ImageController using command given below –

The above command will create a controller name ImageController. Now, open controller app/HTTP/Controller/ImageController and put the following code in it:

5: Create the blade view

Now, we will create some blade views files, Go to app/resources/views/ and create a blade file name image.blade.php and put the following code into your file:

image.blade.php

6: Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

Laravel 7 Crud with Image Upload From Scratch

In this Laravel 7 Crud with Image Upload Example tutorial, I’ll show you how to create basic CRUD application with Image upload in laravel. In this tutorial you learn to create simple CRUD application with image upload. In this step by step tutorial we will be creating simple crud application with image upload in laravel.

Laravel 7 Crud with Image Upload From Scratch

  • Install Laravel Fresh Setup
  • Setup Database Credentials
  • Generate Model and Migration
  • Create Resource Route & Controller
  • Create the blade view
  • Start Development Server

Step 1: Install Laravel Fresh Setup

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

Step 2: Setup Database Credentials

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.

Step 3: Generate Model and Migration

Now, in this step we will create model and migration file. Please run the following command:

The above command will create a model name Product and a migration file for Product table. Now, go to database/migrations file and put the below here :

Now, run the migration to create database table using following artisan command:

If you found any query builder error in command prompt go to => app\Providers\AppServiceProvider.php and put the below code here :

And then run this below command :

Now, add the fillable property inside Product.php file.

4). Create Resource Route & Controller

In this step we will create a resource route and controller using following command:

The above command will create a controller name ProductController with default methods like index, store, create, update, destroy, show, edit. Now we need to add the resource route. Go to routes/web.php put the below routes here :

Next open controller, Go to app/HTTP/Controller/ProductController and put the below code here :

Step 5: Create the blade view

Create the product directory with inside following blade view file for CRUD operations:

  1. Layout.blade.php
  2. List.blade.php
  3. Create.blade.php
  4. Edit.blade.php

Layout.blade.php

List.blade.php

Create.blade.php

Edit.blade.php

Step 6: Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

Laravel 7/6 socialite Linkedin Login Example

In this Laravel 7/6 socialite Linkedin Login tutorial I’ll show you how to integrate socialite LinkedIn social login in laravel application. In this tutorial you will learn to integrate LinkedIn login in laravel. In this article we will integrate login  with LinkedIn in laravel application. This tutorial is step by step guide for you on how to integrate linkedin social login in laravel using socialite package.

As we all know that users are not much interested in filling up long registration form to register with any application. Allowing users to login with their social media accounts is quick and powerful way to get registered/verified users for your laravel application. Allowing users to login with their social media accounts makes registration/login process much easier, it also encourages more users to register for your application. In this step by step tutorial, you will learn to integrate linkedin login with your laravel application.

Laravel 7/6 socialite Linkedin Login Example

  • Step 1: Install Laravel App
  • Step 2: Connect App to Database
  • Step 3: Install Socialite Package
  • Step 4: Create a Linkedin App
  • Step 5: Add Code in Migration, Model File
  • Step 6 Run Migration
  • Step 7: Add Routes for Linkedin Login
  • Step 8: Create Controller
  • Step 9: Generate Bootstrap Auth File
  • Step 10: Add Linkedin Login In Blade View
  • Step 11: Run Development Server

Step 1: Install the laravel App

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

Step 2: Connect App to Database

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.

Step 3: Download Socialite Package

In this step we will Install Socialite Package via Composer using following command:

After Installing ‘socialite’ package, we need to add service provider and alias in config/app.php file as following.

config/app.php

Step 4: Create a Linkedin App

In this step, we will create linkedin app for client and secret key, go to following link

Now create linkedin app filling the details and create your linkedin app. After creating the app set the redirect URL. Now, copy the client id and secret from linkedin app dashboard. Now, go to your project config directory and open service.php file. Then add linkedin app details in service.php file as following:

Step 5: Add Code in Migration, Model File

Now, go to database/migrations directory and open create_users_table.php. Then put the following code into it:

Go to app/User.php and add fillable property like following in User.php model file:

Step 6: Run Migration

Before you run PHP artisan migrate command go to app/providers/AppServiceProvider.php and put the following code in it:

Now, in this step we will create model and migration file. Please run the following command:

Step 7: Add Routes for Linkedin Login

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Step 8: Create Controller

Now, lets create a controller named LinkedinSocialController using command given below –

After creating controller go to app/http/controllers directory and open LinkedinSocialController.php file. And put the following code into LinkedinSocialController.php file:

Step 9: Generate Bootstrap Auth File

Install Laravel UI

Create Auth

NPM Install

Step 10: Add Linkedin Login In Blade View

In this step, you need to add two buttons in laravel blade view for social LinkedIn login. So go to Resources/Views/Auth/register.blade.php and add a Linkedin social login button

Then, go to Resources/Views/Auth/login.blade.php and add a LinkedIn social login button:

Step 11: Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

Laravel 7/6 Login Registration Logout Example

In this Laravel 7/6 login, registration, logout example tutorial I will show you how to use laravel default login registration logout in laravel application. In this tutorial you will learn to create your own laravel login, registration, forget password and reset password functionality. This tutorial is complete laravel authentication system guide that helps you to implement laravel default authentication in laravel application.

Laravel 7/6 Login Registration Logout Example

  • First Install Laravel Fresh Project
  • Configuration changes in .env file
  • Generate Laravel Application Key
  • Database Migration
  • Create a Login Authentication  Laravel
  • Run the Development Server

1. First Install Laravel Fresh Project

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

2. Configuration changes in .env file

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.

3. Generate Laravel Application Key

Now we will generate laravel application key using following command:

4. Database Migration

Before you run php artisan migrate command. Go to the app/providers/AppServiceProvider.php and put the following code in boot method

5. Laravel Authentication

Now, we will enable laravel default authentication using following command:

The above command will generate default authentication routes, controllers and views files for Laravel Login Authentication and registration. After that you can run following command and check ui commands info.

You can use following commands for creating auth:

Using Vue:

Using React:

Now you need to run following npm command, otherwise you will not see layout of login and register page.

Install NPM:

6. Run the Development Server

laravel authentication is successfully completed, Now Go to the browser and hit the

If you did not run PHP artisan server command, direct go to your browser and type the URL

Laravel 7 Ajax Get Data From Database

In this Laravel ajax get data from the database tutorial I will show you how to fetch data from database using ajax in laravel applications. In this tutorial you will learn to use ajax in laravel. In this article I will demonstrate you how to fetch records from database using ajax in laravel. Using ajax you can fetch data from database without reloading page in laravel. In this step by step guide you will understand laravel ajax request implementation.

Laravel 7 Ajax Get Data From Database

  • First Install New Laravel Setup
  • Configure .env file
  • Create One Model and Migration
  • Make Route
  • Generate Controller by command
  • Create Blade View
  • Start Development Server

Step 1: First Install New Laravel Setup

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

Step 2: Configure .env file

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.

Next go to app/datatabase/migrations and open users migration file and put the below code here :

Before we run php artisan migrate command go to app/providers/AppServiceProvider.php and put the below code :

Now, in this step we will create model and migration file. Please run the following command:

Step 3: Make Route

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Step 4: Generate Controller by Command

Now, lets create a controller named AjaxController using command given below –

Next we will use only four methods the from resource controller. Next open your controller and replace the methods given in below :

Step 5: Create blade view :

In this step, we will create a blade file name get-ajax-data.blade.php. Now, go to resource/views/ and create a blade file and put following code in it:

Step 6: Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

How to Use try catch In laravel Example Tutorial

In this How to Use try catch In laravel Example TutorialI will show you How to Use try catch In laravel.In this tutorial you will learn to use try catch exception handling in laravel framework. While working or developing application with Laravel framework, you may encounter many errors. It is very important to handle these errors properly in order to build safe and error free laravel application. In laravel you can use try catch statement for proper error handling in laravel.

How to Use try catch In laravel Example Tutorial

Below is the syntax that represents the try..catch statement:


The try…catch statement is used to handle the errors.

Find Product By Title

Let’s take a look example of laravel try catch. Here you have product table and find the product with it’s title. So you have the following things:

routes/web.php

 

Controller with two methods:

Blade view files as following:

index.blade.php

search.blade.php

Get Errors

In the above example if the product is found, there is no error but if a product is not found, we will encounter some errors on your search.blade.php file.

Now, go to .env file and set

Then the browser will just show blank screen, looks like something went wrong. But that still doesn’t give any valuable information to our visitor. If the product is not found there will error occurs, so you can pass errors on your search.blade.php file using try..catch statement as following:

we can display an error message in Blade file as following

Laravel Livewire Form Tutorial

In this laravel livewire form tutorial, I’ll show you how to install and use laravel liveware package in your laravel application. In this tutorial you will learn to install, setup and use laravel livewire form. In this article I will guide you through how to submit and store form data using the laravel livewire form package in laravel.

Laravel Livewire Form Tutorial

  • Step 1: Install Laravel
  • Step 2: Add Database Detail
  • Step 3: Generate Migration and Model by Command
  • Step 4: Install Livewire
  • Step 5: Create Component
  • Step 6: Create Route
  • Step 7: Create View File
  • Step 8: Run Development Server

Step 1: Install Laravel

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

Step 2: Add Database Detail

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.

Step 3: Generate Model and Migration By Command

Now, in this step we will create model and migration file. Please run the following command:

The above command will create a model name Contact and also create a migration file for Contact table. After successfully run the command go to database/migrations file and put the below here :

now go to app\Providers\AppServiceProvider.php and put the below code here :

Now run the following command to create the table into your database:

Next, Open App/Contact.php file and add the fillable properties:

Step 4: Install Livewire

In this step, we will install livewire Package via the composer dependency manager. Use the following command to install livewire Package.

Step 5: Create Component

In this step we will create a component using following artisan command:

You can see a created file on the following path:

Now, put the following code into your files, so follow the given path and update the code:

app/Http/Livewire/ContactForm.php

resources/views/livewire/contact-form.blade.php

Step 6: Create Route

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Step 7: Create View File

In this step, we will create a blade files that name form.blade.php file inside resources/views/ folder and put the following code into your file:

resources/views/form.blade.php

Step 8: Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

Laravel 7 Custom Validation Error Messages Example

In this laravel 7 custom validation error messages example tutorial I will show you how to implement custom validation message error in laravel projects. In this tutorial you will learn to customize default validation message and use custom validation error messages in laravel forms. In this step by step guide I will demonstrate you how to use custom validation error messages on laravel forms.

Laravel 7 Custom Validation Error Messages Example

  • Step 1: Install Laravel Application
  • Step 2: Setup Database Credentials
  • Step 3: Run Migration Command
  • Step 4: Create Route
  • Step 5: Generate Controller By Command
  • Step 6: Create the blade view
  • Step 7: Run Development Server

Step 1: Install Laravel Application

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

Step 2: Setup Database Credentials

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.

Step 3: Run Migration Command

Now, in this step we will create model and migration file. Please run the following command:

Step 4: Create Routes

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Step 5: Generate Controller By Command

Now, lets create a controller named CustomErrorController using command given below –

Now, go to app/Http/Controllers/CustomErrorController.php and put the following code into your controller file:

app/Http/Controllers/CustomErrorController.php

Step 6: Create Blade View

Now, we will go to resources/views folder and create a blade view file name from.blade.php and put the following code into your file:

resources/views/form.blade.php

Step 7: Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

How to Set or Increase Session Lifetime in Laravel

In this How to Set or Increase Session Lifetime in Laravel I will show you how to set or increase the session timeout or session lifetime in your laravel project. In laravel the session-timeout defines the default session timeout interval for all sessions. The session timeout is defined in minutes. In this tutorial you will learn following topics:

  • How to Set or Increase Session Lifetime in Laravel
  • What is default session time in laravel?
  • How does laravel session expire?

How to Set or Increase Session Lifetime in Laravel

In laravel if you want to set session lifetime for 1 year you can do this as following:

How to Set or Increase Session Lifetime in Laravel

Using .env File

If you set the session timeout in the .env file. The config directory has a session.php. In this file, the session timeout of your laravel web application is set in this way.

Using Config File

O open config/session.php file and set session timeout as following:

What is default session time in laravel?

The cookie_lifetime specifies the lifetime of the cookie in seconds which is sent to the browser. The value 0 means “until the browser is closed.” Defaults to 0.

How does laravel session expire?

If you want laravel session to immediately expire on the browser closing then set that option. | */ ‘lifetime’ => 4320, ‘expire_on_close’ => false, if you want to set session lifetime per user, you need to set this value before logging in the user.

Laravel Logout on Session Expire

In this Laravel Logout on Session Expire tutorial I’ll show you how to logout user’s on session expires. In this tutorial, you will learn to logout user and redirect to the login page when user session timeout or expired. In laravel you can also schedule a task using cron job and artisan command to auto-logout when session expired/session timeout and redirect user’s.

Laravel Logout on Session Expire

Step 1: Create Middleware file

This command will create a middleware name SessionExpired.php. Next find app/Http/Middleware/SessionExpired.php & update the following code into your middleware file:

Step 2: Register the Middleware in Kernal file

In this step, Visit app/Http directory and open a file name Kernel.php & put the below code.

Now open your browser put the below URL: