Category Archives: Blog

Blog

Laravel 5.8 Autocomplete Search Using Typeahead JS

Laravel 5.8 Autocomplete Search Using Typeahead JS

In this article, I will show you how to create a dynamic database driven ajax jquery autocomplete using typeahead js in Laravel. In this tutorial we will create a dynamic search dropdown autocomplete which will fetch options from database table using bootstrap typeahead js.

The Typeahead JS is a jquery plugin, it is used to create dynamic autocomplete input with several options. In this example you will learn how to implement typeahead autocomplete in laravel.

Before starting with example I assume that you already have fresh laravel 5.8 installation ready, if you have not installed it yet you can follow laravel 5 installation instruction here.

Install Laravel 5.8

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

Configure Database 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.

.env

Create Model and Migration

Now, we have to define table schema for contact table. Open terminal and let’s run the following command to generate a Contact model along with a migration file to create contact table in our database.

Once this command is executed you will find a migration file created under “database/migrations”. Lets open migration file created and put following code in it –

Run Laravel Migration

Now, run following command to migrate database schema.

After, the migration executed successfully the contact table will be created in database.

Create AutoComplete Controller

Next, we have to create a controller for AutoComplete suggestion. Create a controller named AutoCompleteController using command given below –

Once the above command executed, it will create a controller file AutoCompleteController.php in app/Http/Controllers directory. Open the AutoCompleteController.php file and put the following code in it.

app/Http/Controllers/AutoCompleteController.php

Create Blade / View Files

In this step, we will create view/blade file to perform autocomplete Search Using Typeahead JS. Lets create a blade file “search.blade.php” in “resources/views/TypeaheadAutocomplete/” directory and put the following code in it respectively.

resources/views/TypeaheadAutocomplete/search.blade.php

Create Routes

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

routes/web.php

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 –

http://localhost:8000/search

Output 1:-

laravel-5-autocomplete-search-using-typeahead-js-1

Output 2:-

laravel-5-autocomplete-search-using-typeahead-js-2

 

Create REST API With Passport Authentication In Laravel 5.8

Laravel 5.8 Create REST API With Passport Authentication

In this tutorial, i will show you how to create rest api in laravel 5.8 application with passport authentication. In this tutorial we will be using passport for api authentication. we will create register and login api with simple retrieve user details.

Laravel comes with default login authentication, but when we want to create APIs we have to use tokens instead of sessions for authenticating users, as APIs does not support session variables. After login via API, user must be assigned a token and sent back to the user which is further used for authenticating API requests. Laravel provides Passport authentication which makes it easy creating REST APIs in laravel.

Install Laravel 5.8

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

Configure Database 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.

Install Passport Package

In this step, we need to install Laravel Passport package via the composer dependency manager. Use the following command to install passport package.

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

config/app.php

Run Migration and Install Laravel Passport

After successfully installing ‘laravel/passport’ package, we require to create default passport tables in our database. so let’s run the following command to migrate Laravel Passport tables to your database.

Now, it is mandatory to install passport using the command below. This command will generate encryption keys required to generate secret access tokens.

Laravel Passport Configuration

Now, we need to make following changes in our model, service provider and auth config file to complete passport configuration. Open App/User.php model file and add ‘Laravel\Passport\HasApiTokens’ trait in it.

Next Register passport routes in App/Providers/AuthServiceProvider.php, open App/Providers/AuthServiceProvider.php and put “Passport::routes()” inside the boot method like below.

Now open config/auth.php file and set api driver to passport instead of session.

Create API Routes

Let’s create API routes. Open routes/api.php file create api routes as below –

Create Authentication Controller

Now, create a Authentication Controller name AuthController. Use the below command to create controller.

Once the above command executed, it will create a resource controller file “AuthController.php” in “app/Http/Controllers/Api” directory. Go to “AuthController.php” and put the following code in it.

Start Application Server

Lets start the development server using following artisan command –

Testing the REST API

Our Authentication API is ready to test. I will use Postman to test the API.

Register Api :-
Verb: POST
URL :http://localhost:8000/Api/v1/register

Login Api :-
Verb: GET
URL :http://localhost:8000/Api/v1/login

getUser Api :-
Verb: GET
URL: http://localhost:8000/Api/v1/getUser

When testing getUser API it requires user to be authenticated, you need to specify headers. Make sure in getUser api we will use following headers as listed bellow.

Basically, you have to specify access token as a Bearer token in the Authorization header. The access token is what you received after login and registration.

Laravel 5 Intervention Image Upload and Resize Example

Laravel 5 Intervention Image Upload and Resize Example

In this Laravel Intervention Image Upload example, we will learn how to upload and resize image using Intervention Image Packag. In this tutorial I have used Intervention Image Package to upload and resize the image and then save image into the database. This laravel image upload example is works with laravel version 5.7 & 5.8 .

Intervention Image Package :- Intervention Image Package is an open source laravel composer package used to upload and resize images. Intervention Image package allows you to easily upload and resize image. Intervention Image package make image uploading and resizing much easier. The advantage of using intervention/image package is that it maintain the image quality. It means you can easily upload and resize images without losing its quality. Laravel image intervention is compatible with laravel version 5.7 & 5.8 .

In this laravel image upload example, I’ll show you how to upload image into folder and then save it into database. In this tutorial before saving image into database we will resize the image and create it’s thumbnail image and then save it into thumbnail directory using the image intervention package.

Install Laravel 5.8

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

Configure Database 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.

.env

Install Image Intervention Package

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

Register Package

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

config/app.php

Generate Migration

Now, we have to define table schema for photos table. Open terminal and let’s run the following command to generate a migration file to create photos table in our database.

Once this command is executed you will find a migration file created under “database/migrations”. lets open migration file and put following code in it –

Run Migration

Now, run following command to migrate database schema.

After, the migration executed successfully the photos table will be created in database along with migrations, password_resets and users table.

Create Model

Next, we need to create a model called Photo using below command.

Once, the above command is executed it will create a model file Photo.php in app directory.

Create Controller

Next, we have to create a controller for image uploading and resizing. Create a controller named ImageController using command given below –

Once the above command executed, it will create a controller file ImageController.php in app/Http/Controllers directory. Open the ImageController.php file and put the following code in it.

app/Http/Controllers/ImageController.php

Here In the controller, we have following methods –

index() :- It displays Image Upload Form along with Uploaded Image

resizeStore() :- To Upload and Resize Image with Intervention Package.

Note:- Before uploading any file make sure you have created following two directory in the public folder called profile_images and /profile_images/thumbnail.

Create Blade / View Files

In this step, we will create view/blade file to generate and display Image Upload Form. Lets create a blade file “index.blade.php” in “resources/views/ImageIntervention/” directory and put the following code in it respectively.

resources/views/ImageIntervention/index.blade.php

Create Routes

After this, we need to add following routes in “routes/web.php” file along with a resource route. Lets open “routes/web.php” file and add following route.

routes/web.php

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 –

http://localhost:8000/intervention-image-upload

Output:-

laravel-5-intervention-image-upload-1

After Image Upload Screen Output:-

laravel-5-intervention-image-upload-2

 

Laravel 5.7 Google ReCAPTCHA Integration

Laravel 5.7 Google ReCAPTCHA Integration

Google ReCaptcha is a one of the most popular captcha system that defends your site from bots to spam and abuse. The Google ReCaptcha is an open service that assures a computer user is a human. In order to integrate the reCaptcha, you must sign up for the Google API key & secret for your website.

In this tutorial, we will integrate google recaptcha in laravel 5.7application. I’ll guide you through step by step how to integrate google recaptcha in a laravel 5.7 application. We will be using ‘anhskohbo/no-captcha’ package for google recaptcha integration in laravel 5.7 application. In this tutorial, we will be creating a simple form with some basic fields along with the google recaptcha.

Contents

  • Install Laravel 5.7
  • Setup Database Credentials
  • Install Google Captcha Package
  • Set Google Site Key and Secret Key
  • Define Route
  • Create Controller
  • Create Blade View (form)
  • Start Development Server
  • Conclusion

Install Laravel 5.7

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

Make sure you have composer installed.

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.

Install Google Captcha Package

We will be using ‘anhskohbo/no-captcha’ package for google recaptcha integration in laravel 5.7 application. In this step we will be installing “anhskohbo/no-captcha” via following composer command, lets open your terminal and switch to the project directory and enter the following command –

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

config/app.php

Set Google Site Key and Secret Key

Now, we need to set Google Site Key and Secret Key in .env file. Let’s start by getting Google key & secret required to make ReCaptcha authorize with the Google servers. Visit the following link to register your site –

Here, you need to provide label to identify the site. Now choose the type of reCAPTCHA as reCAPTCHA V2. Now you need to add the list of domains, this captcha would work on.

laravel_5_8_google_recaptcha_integration_2

Now accept the Terms of service and click Register. Now, you will be presented with your key and secret, add these to your application’s .env file.

Let’s open .env file and add this crendential as following –

.env

Create Controller

Now, lets create Recaptcha Controller using following command

Once the above command executed, it will create a controller file RecaptchaController.php in app/Http/Controllers/ directory. Open the RecaptchaController.php file and put the following code in it.

Controllers/RecaptchaController.php

Create View/Blade File

In this step, we will create view/blade file to render a register form with recaptcha field. Lets create a “create.blade.php” file in “resources/views/recaptcha/” directory and put the following code in it.

resources/views/recaptcha/create.blade.php

Set Routes

After this, we need to add following two routes in “routes/web.php” to display register form and to post the form data. Lets open “routes/web.php” file and add following route.

routes/web.php

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 –

http://localhost:8000/larablog/recaptchacreate

Output:-

laravel_5_7_google_recaptcha_integration_3

Laravel 5.8 Facebook Login with Socialite

Laravel 5.8 Facebook Login with Socialite

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 Facebook login with your laravel application.

Install Laravel 5.8

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

Configure Database 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.

Generate Application Key

Open terminal and switch to the project directory and run the following command to generate application key and configure cache.

Set Default String Length

Locate the file “app/Providers/AppServiceProvider”, and add following line of code to the top of the file

and inside the boot method set a default string length as given below –

So this is how “app/Providers/AppServiceProvider” file looks like –

Install Socialite Package via Composer

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

config/app.php

Crate Facebook Login App

Go to Facebook’s developers site https://developers.facebook.com/ and log in with your Facebook account. There in Facebook developers Dashboard you have option to Create App. Lets create a Facebook Login App simply providing App Name and Contact Email.

laravel-5-8-facebook-login

Set Redirect URL

After creating the facebook app, go to setting->advanced and set redirect url like below –

laravel-5-8-socialite-facebook-login-set-redirect-url-1

Then, go to Facebook Login -> Settings and scroll down to “Valid OAuth Redirect URL” and set your callback URL like below –

laravel-5-8-socialite-facebook-login-set-redirect-url-2

Get Facebook App Id and Secret Key

laravel-facebook-login-set-redirect-url-3

After creating app, go to Settings -> Basic and get your newly created App ID and App Secret.

Configure Laravel Socialite

Open your application .env files and set FB_CLIENT_ID, FB_CLIENT_SECRET and FB_CALLBACK_URL like below –

.env

Here, Your App Secret becomes client_secret, and App ID becomes your client_id. Now, open config/service.php file and set client id and client secret with call back url like below –

config/service.php

Migrations and User Model

After installing laravel, you will see the some migration files created inside database/migrations directory for creating default create users table and password reset table. Open app/database/create_users_table.php migration file and add provider and provider_id columns to user table and also set the email and password field to nullable like below –

app/database/xxxx_xx_xx_xxxxxx_create_users_table.php

After you added columns provider and provider_id to user table set the email and password field to nullable, run the migration using following command.

After this, go to app/User.php and add provider and provider_id to fillable attribute in the User Model.

Authentication Scaffolding

Laravel comes with in-built basic authentication system, use the below command to generate default authentication scaffolding –

This command will generate required Controller files, views and add routes in our web.php routes file that are required for basic authentication system.

Now, open app/routes/web.php file and add following two routes for redirecting the user to the OAuth provider and to handle the callback from provider.

app/routes/web.php

Create Authentication Controllers

Next, we have to create a controller to handle facebook login. Create a controller named SocialController using command given below –

When controller is created successfully go to app/controllers/SocialController.php and put the below code.

app/controllers/SocialController.php

In this controller, we have following two methods –

redirect() :- to redirect the user to Facebook

callback() :- to handle callback from Facebook

No, open resources/views/auth/login.blade.php file and add the following code below the closing tag for <div class=”form-group”>. This will append a facebook login button just below the default login form.

resources/views/auth/login.blade.php

Restart Development Server

Restart the development server using following artisan command –

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

http://localhost:8000/

Output:-

laravel-5-8-socialite-facebook-loginTest Laravel 5.8 Facebook Login

Now, visit the login page to test the facebook login –

http://localhost:8000/login

laravel-5-8-socialite-facebook-login-page

When you click “Facebook” button, you will be redirected/requested to login with facebook. After you successfully login you will be registered and logged in to your application and you will be redirected to user dashboard.

Laravel 5.8 User Registration And Login System

Laravel 5.8 User Registration And Login System

Laravel comes with an built-in authentication system, that includes out of the box user registration, login, logout, forgot password and remember me functionality. It saves us a lot of time building a custom login and registration system starting from scratch. In this step by step tutorial, we will guide you through building user registration and login using laravel’s built-in authentication package.

Before starting with tutorial, we are assuming that you already have a fresh installation of a Laravel 5.8. If you have not installed it follow Laravel Installation Step.

Install Laravel 5.8

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

Make sure you have composer installed. Now, lets switch to the project directory and start the development server using following artisan command –

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

http://localhost:8000/

Output:-

laravel-5-7-crud-tutorial-with-example-2

Configure Database 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.

Generate Laravel Application Key

Now, use the following command in terminal to generate laravel application key.

Create Database Migration

Before you run migration, open app/providers/AppServiceProvider.php file and put following two line of code in boot method as following –

Now, use the below command to create required user authentication tables and model automatically.

Authentication Scaffolding

Now, use the below command to generate authentication scaffolding –

This command will generate required Controller files, views and add routes in our web.php routes file that are required for the authentication. If you open your route file (resources/routes/web.php) you can see the authentication route added as following –

The Auth::rutes() is a set of common authentication, registration and password reset routes.

Restart Development Server

Restart the development server using following artisan command –

Test Laravel 5.8 User Authentication

Now, you can see following authentication pages –

http://localhost:8000/register

laravel_user_registration_login_authentication_register

http://localhost:8000/login

laravel_user_registration_login_authentication_login

http://localhost:8000/password/reset

laravel_user_registration_login_authentication_forgot_password

Laravel 5.8 Google ReCAPTCHA Integration

Laravel 5.8 Google ReCAPTCHA Integration

Google ReCaptcha is a one of the most popular captcha system that defends your site from bots to spam and abuse. The Google ReCaptcha is an open service that assures a computer user is a human. In order to integrate the reCaptcha, you must sign up for the Google API key & secret for your website.

In this tutorial, we will integrate google recaptcha in laravel 5.8 application. I’ll guide you through step by step how to integrate google recaptcha in a laravel 5.8 application. We will be using ‘anhskohbo/no-captcha’ package for google recaptcha integration in laravel 5.8 application. In this tutorial, we will be creating a simple form with some basic fields along with the google recaptcha.

Contents

  • Install Laravel 5.8
  • Setup Database Credentials
  • Install Google Captcha Package
  • Set Google Site Key and Secret Key
  • Define Route
  • Create Controller
  • Create Blade View (form)
  • Start Development Server
  • Conclusion

Install Laravel 5.8

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

Make sure you have composer installed.

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.

Install Google Captcha Package

We will be using ‘anhskohbo/no-captcha’ package for google recaptcha integration in laravel 5.8 application. In this step we will be installing “anhskohbo/no-captcha” via following composer command, lets open your terminal and switch to the project directory and enter the following command –

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

config/app.php

Set Google Site Key and Secret Key

Now, we need to set Google Site Key and Secret Key in .env file. Let’s start by getting Google key & secret required to make ReCaptcha authorize with the Google servers. Visit the following link to register your site –

Here, you need to provide label to identify the site. Now choose the type of reCAPTCHA as reCAPTCHA V2. Now you need to add the list of domains, this captcha would work on.

laravel_5_8_google_recaptcha_integration_2

Now accept the Terms of service and click Register. Now, you will be presented with your key and secret, add these to your application’s .env file.

Let’s open .env file and add this crendential as following –

.env

Create Controller

Now, lets create Recaptcha Controller using following command

Once the above command executed, it will create a controller file RecaptchaController.php in app/Http/Controllers/ directory. Open the RecaptchaController.php file and put the following code in it.

Controllers/RecaptchaController.php

Create View/Blade File

In this step, we will create view/blade file to render a register form with recaptcha field. Lets create a “create.blade.php” file in “resources/views/recaptcha/” directory and put the following code in it.

resources/views/recaptcha/create.blade.php

Set Routes

After this, we need to add following two routes in “routes/web.php” to display register form and to post the form data. Lets open “routes/web.php” file and add following route.

routes/web.php

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 –

http://localhost:8000/larablog/recaptchacreate

Output:-

laravel_5_8_google_recaptcha_integration_3

Laravel 5.8 New Email Verification

Laravel 5.8 Email Verification

Laravel 5.8 provides built-in email verification system for newly registered user. Using this, newly registered user will get an email with an account activation link. This activation link is used for account verification. When activation link is clicked, this will make user account verified and active for the application. Once user account is activated then it will be allowed to access protected routes; which can be accessible only by verified accounts.

In this step by step tutorial, we’ll show you how to setup email verification system for newly registered user in laravel 5.8.

Install Laravel 5.8

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

Make sure you have composer installed. Now, lets switch to the project directory and start the development server using following artisan command –

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

http://localhost:8000/

Output:-

laravel-5-7-crud-tutorial-with-example-2

Configure Database 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.

Now, use the below command to create default laravel tables automatically.

Email Configuration

Since, we will be sending an email with an account activation link. To send email, we need to add email configuration or smtp details in .env file.

Authentication Scaffolding

Now, use the below command to generate authentication scaffolding –

This command will generate required Controller files, views and add routes in our web.php routes file that are required for the authentication. If you open your route file (resources/routes/web.php) you will have a resource route added in it like below –

Email Verification Setup

To implement email verification we need to implement the MustVerifyEmail Contracts in our User model. Lets open the App/User.php file and add the “Implements MustVerifyEmail” to the class declaration as given below –

Add Route

Last thing we need to do is add the verify middleware to the auth routes.

The “verified” middleware is used to protect routes from unverified users.

We can add the “verified” middleware to the route itself as given below –

Example:-

or we can also use it in controllers as following –

Example:-

Add Middleware In Controller

In this example we will be adding Middleware in controller’s constructor to protect access. Lets open app/controllers/HomeController.php and put $this->middleware([‘auth’, ‘verified’]); inside of constructor.

app/controllers/HomeController.php

This will block unverified users to access laravel 5.8 dashboard, and only allow when users have verified their email. Now when you register a new user, it will send a verification email to that user and redirect to verify view (same will happen when a old user tries to access home route).

Start Development Server

Start the development server using following artisan command –

Test Laravel 5.8 Email Verification

At this stage, we are ready to test the user registration and login system with email verification system.

Now, you should able to see your registration page at

http://localhost:8000/register

laravel-5-8-email-verification-example-2

When you fill the form and submit it; you will get an alert for email verification in your account as follows –

laravel-5-8-email-verification-example-3-1

You will also get an account verification link in your email account as follows –

laravel-5-8-email-verification-example-4

Go ahead and verify your account, on successful verification you will be allowed to access protected application routes.

laravel-5-8-email-verification-example-5

Laravel 6 Import Export Excel CSV File to Database

Laravel 6 Import Export Excel CSV File to Database

In this tutorial, I’ll show you how to import and export excel/csv file into and from database in laravel 6 using maatwebsite version 3 package with example. I’ll guide you through step by step with example of importing and exporting a csv or excel file using maatwebsite/excel version 3 composer package.

When we are developing laravel applications, there may situations where we require to import or export large amount of data into and from the application database. Importing and exporting data through excel or csv files seems a good solution here.

In laravel, maatwebsite/excel composer package made it easy to import or export excel or csv files. The maatwebsite/excel is a composer package used to import and export excel or csv files. The maatwebsite/excel provide number of features for excel or csv file import and exort in laravel.

Create Laravel 5.8 Application

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

Configure Database 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.

.env

Create Model and Migration

Now, we have to define table schema for contact table. Open terminal and let’s run the following command to generate a Contact model along with a migration file to create contact table in our database.

Once this command is executed you will find a migration file created under “database/migrations”. Lets open migration file created and put following code in it –

Run Laravel Migration

Now, run following command to migrate database schema.

After, the migration executed successfully the contact table will be created in database along with a model file Contact.php in app directory.

app/Contact.php

Install Maatwebsite Package

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

Configure Maatwebsite Package

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

config/app.php

Now, use following command to publish Maatwesite Package configuration file:

This will create Maatwesite Package configuration file named “config/excel.php”.

Create Import Class

Now we will create a import class for Contact model to use in our ImportExportExcelController. Use the following command to create import class.

After, the above command executed successfully it will create ImportContacts.php file in Imports directory.

Imports/ImportContacts.php

Create Export Class

Now we will create a export class for Contact model to use in our ImportExportExcelController. Use the following command to create import class.

After, the above command executed successfully it will create ExportContacts.php file in Exports directory.

Exports/ExportContacts.php

Create Import Excel Controller

Next, we have to create a controller to display a form to upload excel file records. Lets Create a controller named ImportExportExcelController using command given below –

Once the above command executed, it will create a controller file ImportExportExcelController.php in app/Http/Controllers/ImportExportExcel directory.

Open the ImportExportExcel/ImportExportExcelController.php file and put the following code in it.

app/Http/Controllers/ImportExportExcel/ImportExportExcelController.php

Here In the controller, we have following methods –

index() :- It displays File Upload Form and Contact data.

import() :- To Upload excel file and Save records in database .

export() :- To export and download excel file for records in database .

Create Blade / View Files

In this step, we will create view/blade file to upload excel file. Lets create a blade file “index.blade.php” in “resources/views/import_export_excel/” directory and put the following code in it respectively.

resources/views/import_export_excel/index.blade.php

Create Import Export Excel Routes

After this, we need to add following routes in “routes/web.php” file along with a resource route. Lets open “routes/web.php” file and add following route.

routes/web.php

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 –

http://127.0.0.1:8000/import-export-excel

Output:-

laravel-6-import-export-excel-file-in-database-1

Select excel file to upload, before uploading you must have created an excel file as following:

laravel-6-import-excel-csv-file-in-database-1

laravel-6-import-export-excel-file-in-database-2

Now, import file.

After Upload Screen Output:-

laravel-6-import-export-excel-file-in-database-3

Now, click “Export File” to export data into csv file.

laravel-6-import-export-excel-file-in-database-4

I hope you like this laravel 6 Import Export Excel CSV File to Database tutorial.

Laravel 5.8 Import Excel CSV File to Database Using Maatwebsite

Laravel 5.8 Import Excel CSV File to Database

In this tutorial, I’ll show you how to import Excel spreadsheet or csv file into database in laravel 5.8 using maatwebsite version 3 package with example. I’ll guide you through step by step with example of importing a csv or excel file using maatwebsite/excel version 3 composer package.

When we are working with some large applications, there may situations where we require to import large amount of data into the application database. Importing data through excel or csv files seems a good solution here. In laravel, maatwebsite/excel composer package made it easy to import or export excel or csv files.

Maatwebsite version 3 package:-

The maatwebsite/excel is a composer package used to import and export excel or csv files. This package made it easy to import and export data using database model. We will be using maatwebsite/excel version 3, there are many new changes in version 3. It will work with laravel version like 5.8, 5.7 .

Before starting with example I assume that you already have fresh laravel 5.8 installation ready, if you have it installed then you can skip this step.

Create Laravel 5.8 Application

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

Configure Database 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.

.env

Create Model and Migration

Now, we have to define table schema for contact table. Open terminal and let’s run the following command to generate a Contact model along with a migration file to create contact table in our database.

Once this command is executed you will find a migration file created under “database/migrations”. Lets open migration file created and put following code in it –

Run Laravel Migration

Now, run following command to migrate database schema.

After, the migration executed successfully the contact table will be created in database along with a model file Contact.php in app directory.

app/Contact.php

Install Maatwebsite Package

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

Configure Maatwebsite Package

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

config/app.php

Now, use following command to publish Maatwesite Package configuration file:

This will create Maatwesite Package configuration file named “config/excel.php”.

Create Import Class

Now we will create a import class for Contact model to use in our ImportExcelController. Use the following command to create import class.

After, the above command executed successfully it will create ImportContacts.php file in Imports directory.

Imports/ImportContacts.php

Create Import Excel Controller

Next, we have to create a controller to display a form to upload excel file records. Lets Create a controller named ImportExcelController using command given below –

Once the above command executed, it will create a controller file ImportExcelController.php in app/Http/Controllers/ImportExcel directory. Open the ImportExcel/ImportExcelController.php file and put the following code in it.

app/Http/Controllers/ImportExcel/ImportExcelController.php

Here In the controller, we have following methods –

index() :- It displays File Upload Form.

import() :- To Upload excel file and Save records in database .

Create Blade / View Files

In this step, we will create view/blade file to upload excel file. Lets create a blade file “index.blade.php” in “resources/views/import_excel/” directory and put the following code in it respectively.

resources/views/import_excel/index.blade.php

Create Import Excel Routes

After this, we need to add following routes in “routes/web.php” file along with a resource route. Lets open “routes/web.php” file and add following route.

routes/web.php

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 –

http://127.0.0.1:8000/import-excel

Output:-

laravel-5-8-import-excel-1

Select excel file to upload, before uploading you must have created an excel file as following:

laravel-6-import-excel-csv-file-in-database-1

laravel-5-8-import-excel-2

Now, import file.

After Upload Screen Output:-

laravel-5-8-import-excel-3

Laravel 6 Import Excel CSV File to Database Using Maatwebsite

Laravel 6 Import Excel CSV File to Database

In this tutorial, I’ll show you how to import Excel spreadsheet or csv file into database in laravel 6 using maatwebsite version 3 package with example. I’ll guide you through step by step with example of importing a csv or excel file using maatwebsite/excel version 3 composer package.

When we are working with some large applications, there may situations where we require to import large amount of data into the application database. Importing data through excel or csv files seems a good solution here. In laravel, maatwebsite/excel composer package made it easy to import or export excel or csv files.

Maatwebsite version 3 package:-

The maatwebsite/excel is a composer package used to import and export excel or csv files. This package made it easy to import and export data using database model. We will be using maatwebsite/excel version 3, there are many new changes in version 3. It will work with laravel version like 5.8, 5.7 .

In this laravel import excel example, we will display a form to upload csv file and to display uploaded customer data. When the excel file is imported into database we will display imported data into the screen along with the success message.

Before starting with example I assume that you already have fresh laravel 6 installation ready, if you have it installed then you can skip this step.

Create Laravel 6 Application

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

Configure Database 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.

.env

Create Model and Migration

Now, we have to define table schema for contact table. Open terminal and let’s run the following command to generate a Contact model along with a migration file to create contact table in our database.

Once this command is executed you will find a migration file created under “database/migrations”. Lets open migration file created and put following code in it –

Run Laravel Migration

Now, run following command to migrate database schema.

After, the migration executed successfully the contact table will be created in database along with a model file Contact.php in app directory.

app/Contact.php

Install Maatwebsite Package

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

Configure Maatwebsite Package

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

config/app.php

Now, use following command to publish Maatwesite Package configuration file:

This will create Maatwesite Package configuration file named “config/excel.php”.

Create Import Class

Now we will create a import class for Contact model to use in our ImportExcelController. Use the following command to create import class.

After, the above command executed successfully it will create ImportContacts.php file in Imports directory.

Imports/ImportContacts.php

Create Import Excel Controller

Next, we have to create a controller to display a form to upload excel file records. Lets Create a controller named ImportExcelController using command given below –

Once the above command executed, it will create a controller file ImportExcelController.php in app/Http/Controllers/ImportExcel directory. Open the ImportExcel/ImportExcelController.php file and put the following code in it.

app/Http/Controllers/ImportExcel/ImportExcelController.php

Here In the controller, we have following methods –

index() :- It displays File Upload Form.

import() :- To Upload excel file and Save records in database .

Create Blade / View Files

In this step, we will create view/blade file to upload excel file. Lets create a blade file “index.blade.php” in “resources/views/import_excel/” directory and put the following code in it respectively.

resources/views/import_excel/index.blade.php

Create Import Excel Routes

After this, we need to add following routes in “routes/web.php” file along with a resource route. Lets open “routes/web.php” file and add following route.

routes/web.php

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 –

http://127.0.0.1:8000/import-excel

Output:-

laravel-6-import-excel-csv-file-in-database-11

 

Select excel file to upload, before uploading you must have created an excel file as following:

laravel-6-import-excel-csv-file-in-database-1

laravel-6-import-excel-csv-file-in-database-12

Now, import file.

After Upload Screen Output:-

laravel-6-import-excel-csv-file-in-database-13

 

Laravel 5.8 Dropzone Multiple Image Upload with Remove Link

In this Laravel 5.8 Dropzone Multiple Image Upload with Remove Link example, I will show you how to upload files with drag and drop interface using dropzone.js in your Laravel 5 applications. Dropzone is an open source light weight JavaScript library that provides drag and drop features to upload images with preview images. In this example before uploading the image we will display preview of the image along with a remove image link and then save it into directory. In order to use dropzone plugin we need to include the Dropzone javascript and CSS files in your laravel blade file. This tutorial we learn to :-

  • Uploading multiple images with dropzone
  • Saving images with unique file names to database
  • Removing images directly from dropzone preview box

Install Laravel 5.8

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

Configure Database 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.

.env

Generate Migration

Now, we have to define table schema for photos table. Open terminal and let’s run the following command to generate a migration file to create photos table in our database.

Once this command is executed you will find a migration file created under “database/migrations”. lets open migration file and put following code in it –

Run Migration

Now, run following command to migrate database schema.

After, the migration executed successfully the photos table will be created in database along with migrations, password_resets and users table.

Create Model

Next, we need to create a model called Photo using below command.

Once, the above command is executed it will create a model file Photo.php in app directory.

Create Controller

Now, create a controller to upload image. Create a controller named ImageUploadController using command given below –

Once the above command executed, it will create a controller file ImageUploadController.php in app/Http/Controllers/dropzone_image_upload/ directory. Open the ImageUploadController.php file and put the following code in it.

app/Http/Controllers/dropzone_image_upload/ImageUploadController.php

Here In the controller, we have following methods –

index() :- It displays Drozone Multiple Image Upload Form

store() :- To Upload using Dropzone and Save Multiple Image in database.

destroy() :- To delete files from directory and database.

Note:- Before uploading any file make sure you have created following two directory in the public folder called profile_images.

Create Blade / View Files

In this step, we will create view/blade file to display Image Upload Form using Dropzone. Lets create a blade file “index.blade.php” in “resources/views/dropzone_image_upload/” directory.

Include Dropzone Javascript and CSS files

In order to use dropzone plugin we need to include following Dropzone javascript and CSS files in your laravel blade file.

Dropzone CSS :-

In this file we will be adding dropzone.min.css along with the our bootstrap.min.css as following –

Dropzone Javascript :-

Here we will add dropzone.js file along with the jquery.js as following –

Configure Dropzone Plugin with Remove Image Link

Next, we will provide initial configurations for Dropzone plugin.We setup follwoing dropzone options –

maxFilesize:- Maximum Image size that can be uploaded.

renameFile:- It is used to rename the file before uploading.

acceptedFiles:- File types or extension that can be uploaded.

addRemoveLinks:- It is set to true to display Remove button to remove uploaded file.

removedfile:- It is a callback function invoked when image is removed from dropzone.

Now, our blade file will look something like this.

resources/views/dropzone_image_upload/index.blade.php

Create Routes

After this, we need to add following dropzone image upload routes in “routes/web.php” file along with a resource route. Lets open “routes/web.php” file and add following route.

routes/web.php

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 –

http://localhost:8000/dropzone-image-upload

Output:-

laravel-5-8-dropzone-multiple-image-upload-1

After Image Upload Screen Output:-

Select and Drop Multiple Image files to upload using dropzone.

laravel-5-8-dropzone-multiple-image-upload-3