Category Archives: Laravel

Laravel Tutorials

Laravel 8 Send Emails using Office365 Example

In this Laravel 8 Send Mail using Office365 Tutorial I will show you how to send mail using Office365 in laravel 8. In this tutorial you will learn to send mails using Office365 In laravel application. In this post I’ll show you how to integrate Office365 to send email in laravel 8 application.

Laravel 8 provides builtin mailable markdown class to send emails. In Laravel 8 you can use mailtrap, smtp, Mailgun, Postmark, Amazon SES, and sendmail for sending emails. You are required to configure driver details on the .env file.

Laravel 8 Send Emails using Office365 Example

In this step by step tutorial you will understand how to send emails in Laravel using Office365.

  • Step 1 – Install Laravel 8 App
  • Step 2 – Configuration SMTP in .env
  • Step 3 – Create Mailable Class
  • Step 4 – Add Email Send Route
  • Step 5 – Create Directory And Blade View
  • Step 6 – Create Email Controller
  • Step 7 – Run Development Server

Step 1 – Install Laravel 8 App

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

Step 2 – Configuration SMTP in .env

In this step we will configure Office365 smtp settings in .env file as following:

Step 3 – Create Mailable Class

Now, create a mailable class for sending emails, use the following command :

Here you need to add an email template name in build class of the above created notifymail class.


In next step, we will create email template named demoMail.blade.php inside resources/views/emails directory. That’s why we have added view name email.

Step 4 – Add Send Email 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 5 – Create Directory And Blade View

In this step, we will create view/blade file to setup email template. Open resources/views/Email/demoMail.blade.php file, put the following code.

resources/views/Email/demoMail.blade.php

Step 6 – Create Send Email Controller

In this step we will create a new controller SendEmailController with one method to send email. Open your terminal and run following command to create controller:

Then navigate to app/Http/Controllers directory and open SendEmailController.php. Then update the following code into it:

Step 7 – Run Development Server

In this step, use this PHP artisan serve command to start your server locally:


Then open browser and fire the following URL on it:

Laravel 8 Create JSON File & Download From Text

In this Laravel 8 Create JSON File and Download From Text tutorial I will show you how to convert form data to JSON and download json file. I will also show you how to insert and download json format data into MySQL database. In this tutorial you will learn convert form data or text file data into json format and to save text file data from JSON data into MySQL database and download data from mysql database.

Laravel 8 Create JSON File & Download From Text

In this step by step tutorial I will guide you through how to save form or text data into database with json format and to download in laravel 8 application. Please follow the instruction given below:

  • Step 1: Install Laravel Latest Setup
  • Step 2: Setup Database
  • Step 3: Generate migration and model
  • Step 4: Migrate Database
  • Step 5: Add Route
  • Step 6: Create controller
  • Step 7: Create blade view
  • Step 8: Start Development Server

Step 1: Install Laravel Latest Setup

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

Step 2: Setup 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: Migrate Database

Before you run php artisan migrate command go to app/providers/AppServiceProvider.php and update the below code into AppServiceProvider.php file:

Step 4: Generate migration and model

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

The above command will create a model and migration file. Go to app/database/migration and find the migration file name create_tests_table.php and update the following code into it:

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

Step 5: Add 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 6: Create Controller

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

Now, go to app/http/controllers open JsonController and create two method. The first method is to display the form and second method convert form data to json format and save it to database:

Step 7: Create Blade view

In this step we will create blade view file. Now, create a blade view name json_form.blade.php in resources/views directory. Put the below code here :

Step 8: Start 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 8 Download File From URL to 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 8 application. In this tutorial you will learn how to download or display files from public storage folder in laravel 8. In this article I will share example to display and download file from public storage folder in laravel 8.

Laravel 8 Download File From URL to Public Storage Folder

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

Steps 1: 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

Step 2: 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.

Step 3: Create Blade View

Now, go to resources\view folder and create a blade view file named show.blade.php. Then update the following code into it:

In Laravel 5,6,7,8, you can do this for a local file to download it:


You can download external image using copy() function, then send it to user in the response:

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 8 Send SMS Notification to Mobile/ Phone Example

In this Laravel 8 Send SMS Notification to Mobile/ Phone Example tutorial I will show you how to send SMS notification to mobile in laravel 8 application. In this tutorial you will learn to send sms notification to mobile in laravel. In this example we will be using nexmo sms package to send sms in laravel 8 application.

Laravel 8 Send SMS Notification to Mobile/ Phone Example

In this step by step tutorial I will demonstrate you with example how to send sms notification in laravel 8 application using nexmo sms package. Please follow instruction given below:

  • Step 1 – Download Laravel 8 Application
  • Step 2 – Connecting App to Database
  • Step 3 – Install SMS Package
  • Step 4 – Create Route
  • Step 5 – Create Controller By Artisan Command
  • Step 6 – Run Development Server

Step 1 – Download Laravel 8 Application

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

Step 2 – Connecting 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 – Install SMS Pakcage

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

Visit the link given below and create a nexmo account there:

After creating account you will be provided app id and secret key.

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 – Create Controller By Artisan Command

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

Now, go to app/http/controllers and open SendSMSController.php file. And put the following code into 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 –

Laravel 8 Livewire Dependent Dropdown Tutorial

In this Laravel 8 Livewire Dependent Dropdown Example tutorial, I’ll show you how to create dependent dropdown with Livewire in laravel 8. In this tutorial you will learn to implement dependent dropdown with Livewire in laravel 8. In this article we will be creating simple country state city dependent dropdown example to demonstrate dependent dropdown example in laravel.

Laravel 8 Livewire Dependent Dropdown Tutorial

In this step by step Laravel 8 Livewire Dependent Dropdown tutorial I will demonstrate you how to create Livewire Dependent Dropdown in laravel 8 application. Please follow instruction given below:

  • Step 1: Install Laravel 8 App
  • Step 2: Add Database Detail
  • Step 3: Create Migration For File using Artisan
  • Step 4: Create Model File
  • Step 5: Install Livewire Package
  • Step 6: Create Dependent Dropdown Component using Artisan
  • Step 7: Add Route For Livewire Dependent Dropdown
  • Step 8: Create View File
  • Step 9: Run Development Server

Step 1: Install Laravel 8 App

First of all we need to create a fresh laravel project, download and install Laravel 8 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: Create Migration For File using Artisan

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

Now, go to database/migrations folder and open create_states_cities_tables.php file. Then update the following code into create_states_cities_tables.php file:

Step 4: Create Model File

Now, we will create model file. Please run the following command:

Then visit app/Models/ directory and open state.php file and add the following code into it:

Then visit app/Models/ directory and open city.php file and add the following code into it:

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

Step 5: Install Livewire Package

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

Step 6: Create Dependent Dropdown Component using Artisan

In this step we will create a livewire component for country state city dropdown functionality:

The above command will create the component on the following path:

Now, go to app/Http/Livewire folder and open Statecitydropdown.php file. Then add the following code into your Statecitydropdown.php file:


Now, go to resources/views/livewire folder and open statecitydropdown.blade.php file. Then add the following code into your statecitydropdown.blade.php file:

Step 7: Add Route For Livewire Depedent Dropdown

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 View File

In this step we will create blade view files. Go to resources/views/livewire folder and create one blade view files that name app.blade.php file. Then put the following code into your app.blade.php file:

Step 9: 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 8 Livewire Click Event Tutorial Example

In this Laravel 8 livewire click event tutorial example tutorial I will show you how to implement click event with livewire in laravel 8 application. In this tutorial you will learn to implement or create click event using livewire in laravel 8. In this article I will share example to create livewire click event in laravel.

Laravel 8 Livewire Click Event Tutorial Example

In this step by step tutorial I will demonstrate you how to implement click event with livewire in laravel 8 application. Please follow the instruction given below:

  • Step 1: Install Laravel 8 App
  • Step 2: Add Database Detail
  • Step 3: Install Livewire Package
  • Step 4: Create Click Event Component using Artisan
  • Step 5: Add Route For Livewire Click Event
  • Step 6: Create View File
  • Step 7: Run Development Server

Step 1: Install Laravel 8 App

First of all we need to create a fresh laravel project, download and install Laravel 8 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.

Now, run following command to migrate database schema.

Step 3: Install Livewire Package

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

Step 4: Create Click Event Component using Artisan

Now we will create a livewire click event component using following artisan command:

The above command will create the following components on the following path:

Now, go to app/Http/Livewire folder and open ClickEvent.php file. Then add the following code into your ClickEvent.php file:


Now, go to resources/views/livewire folder and open click-event.blade.php file. Then add the following code into your click-event.blade.php file:

Step 5: Add Route For Livewire Click Event

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 6: Create View File

In this step we will create blade view file. Go to resources/views/livewire folder and create one blade view files that name app.blade.php file. Then add the following code into your app.blade.php file:

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 –

Laravel Create Custom Blade Directive

In this Laravel Create Custom Blade Directive tutorial I will show you how to create custom blade directive in laravel. In this tutorial you will learn to create custom blade directives in laravel. In this article I will share example to create custom blade directives.

In laravel you can create your own @var, @if, @case directive that help you to avoid rewrite same code multiple time so that you can reuse.

Laravel Create Custom Blade Directive

In this step by step tutorial we will be creating custom blade directives and reuse it in blade file. we will create @nl2br blade directive and use it with example. Please follow the instruction given below:

Step 1: Create Custom Blade Directive

First of all you have to declare custom blade directive in your app service provide file:

app/Providers/AppServiceProvider.php

Step 2: 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 3: Create Blade File

Now you can use @nl2br directive in this blade file as following:

Laravel 8 Livewire Select2 Dropdown Tutorial Example

In this Laravel 8 livewire select2 example tutorial I will show you how to implement livewire select2 dropdown in laravel 8 application. In this tutorial you will learn to implement livewire select2 dropdown in laravel 8. In this article I will share example to implement livewire select2 dropdown in laravel 8 application.

Laravel 8 Livewire Select2 Dropdown Tutorial Example

In this step by step tutorial I will demonstrate you with example how to create livewire select2 dropdown in laravel 8 application. Please follow the instruction given below:

  • Step 1: Install Laravel 8 App
  • Step 2: Add Database Detail
  • Step 3: Install Livewire Package
  • Step 4: Create Select2 Component using Artisan
  • Step 5: Add Route For Livewire Select2
  • Step 6: Create View File
  • Step 7: Run Development Server

Step 1: Install Laravel 8 App

First of all we need to create a fresh laravel project, download and install Laravel 8 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.

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

Step 3: Install Livewire Package

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

Step 4: Create Select2 Component using Artisan

In this step we will create a livewire component for creating a livewire select2 dropdown component using the following command:

This command will create the following components on the following path:

Now, go to app/Http/Livewire folder and open Select2.php file. Then put the following code into your Select2.php file:


Now, go to resources/views/livewire folder and open select2.blade.php file. Then add the following code into your select2.blade.php file:

Step 5: Add Route For Livewire Select2

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 6: Create View File

In this step we will create blade view file. Go to resources/views/livewire folder and create one blade view files that name app.blade.php file. Then add the following code into your app.blade.php file:

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 –

Laravel 8 Send SMS to Mobile with Nexmo Example

In this Laravel 8 Send SMS to Mobile with Nexmo Example tutorial I will show you how to send SMS message to mobile in laravel 8 application. In this tutorial you will learn to send sms to mobile in laravel. In this example we will be using nexmo sms package to send sms message in laravel 8 application.

Laravel 8 Send SMS to Mobile with Nexmo Example

In this step by step tutorial I will demonstrate you with example how to send sms message in laravel 8 application using nexmo sms package. Please follow instruction given below:

  • Step 1 – Download Laravel 8 Application
  • Step 2 – Connecting App to Database
  • Step 3 – Install SMS Package
  • Step 4 – Create Route
  • Step 5 – Create Controller By Artisan Command
  • Step 6 – Run Development Server

Step 1 – Download Laravel 8 Application

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

Step 2 – Connecting 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 – Install SMS Pakcage

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

Visit the link given below and create a nexmo account there:

After creating account you will be provided app id and secret key.

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 – Create Controller By Artisan Command

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

Now, go to app/http/controllers and open SendSMSController.php file. And put the following code into 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 –

Laravel 8 Find Nearest Location By Latitude and Longitude

In this Laravel 8 Find Nearest Location By Latitude and Longitude tutorial I will show you How to find nearest location using latitude and longitude in laravel 8 application. In this tutorial you will learn How to find nearest location using latitude and longitude in laravel 8. In this article I will share example to find nearest location using latitude and longitude in laravel 8 application. When developing any web application we come to situations where we need to find the nearest location using latitude and longitude in laravel. In this tutorial I will help you find nearest location using your current latitude and longitude in laravel 8. In this examplewe will find neareby place using latitude and longitude query in laravel eloquent. You can get nearest geolocation by mysql radius query laravel 8.

Laravel 8 Find Nearest Location By Latitude and Longitude

In this step by step tutorial I will demonstrate you How to find nearest location using latitude and longitude in laravel 8. Please follow the instruction given below:

  • Step 1 – Install Laravel 8 App
  • Step 2 – Connecting App to Database
  • Step 3 – Add Route
  • Step 4 – Generate Controller by Command
  • Step 5 – Run Development Server
  • Step 6 – Test This App

Step 1 – Install Laravel 8 App

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

Step 2 – Connecting 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 – Add 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 LocationController using command given below –

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

Step 5 – 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 8 Generate and Read XML File Tutorial Example

In this Laravel 8 Generate and Read XML File Tutorial Example tutorial I will show you how to read xml file in laravel 8 application. In this tutorial you will learn to generate and read xml file in laravel 8 application. In this article I will share example to generate and read xml file in laravel 8 application.

Laravel 8 Read XML File Tutorial Example

In this step by step tutorial I will demonstrate you how to read xml file in laravel. Please follow the instruction given below:

Create Sample XML File

Please create a sample xml file with below code and save it as name “sample-course.xml” and save it in public directory:

Install Laravel 8

First of all we need to create a fresh laravel project, download and install Laravel 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.

Create Controller

In this step we will create a controller to parse or read xml data from file using following command:

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

app/Http/Controllers/ReadXmlController.php

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

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 Eloquent firstWhere() Example

In this Laravel Eloquent firstWhere() Example tutorial I will show you how to use Eloquent firstWhere() method in laravel. In this tutorial you will learn about the use case and how to use Eloquent firstWhere() method in laravel model query. In this article I will share various example to use Eloquent firstWhere() method in laravel application.

What Is Eloquent firstWhere() method?

The eloquent firstWhere() method returns the first element in the collection with the given key / value pair. In laravel eloquent firstWhere() method will help you to easily fetch match first record.

Laravel Eloquent firstWhere() Example

The FirstWhere method returns the first element in the collection with the given key / value pair.

Example 1:

Output

Example 2:

Output

Example 3:

Output: