Category Archives: Laravel

Laravel Tutorials

Laravel 8 Yajra Datatables Example Tutorial

In this Laravel 8 Yajra Datatables Example Tutorial, I’ll show you how to install and use yajra DataTables in your laravel 8 application with example. In this tutorial you will learn to integrate yajra DataTables in your laravel 8 application. We will be using yajra datatable package for listing of records with pagination, sorting and filter (search) feature. Laravel Yajra datatables package comes with many built-in features for searching and sorting functionality.

Laravel 8 Yajra Datatables Example Tutorial

In this step by step Laravel 8 Yajra Datatables Example Tutorial I will demonstrate you how to install and use yajra DataTables in your laravel 8 application. I will also share example to use yajra DataTables in your laravel 8 application. Please follow the instruction given below:

Step 1: Install Laravel 8

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

Step 2 : Install Yajra Datatable

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

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

config/app.php

Step 3: Add Dummy Records

Now, we need to add some fake records in the database.

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

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

app/Http/Controllers/UserController.php

Step 6: Create View

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

resources/views/users.blade.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 8 Ajax Form Validation Tutorial

In this Laravel 8 Ajax Form Validation Tutorial I’ll show you how to submit form using ajax and validate form data in laravel 8 application. In this laravel form validation tutorial you will learn to submit and validate form using ajax in laravel form with built in laravel validation available in laravel. In this Laravel 8 ajax form validation tutorial I’ll share various validation implementation and use case. In this laravel ajax form validation tutorial we will be learning about laravel server side validations using ajax form submit. In this tutorial, I’ll show you how to submit and validate form data using ajax and to display validation errors.

Laravel 8 Form Validation

Laravel 8 comes many built-in validation rules that can be easily implemented. In this example, we have used some of following validation rules –

required:- It set input field is required.
min:- Validate minimum character length.
max:- Validate maximum character length.
email: It set input must be a valid email address.
unique: It checks for unique value against a database column.
numeric: Input value must numeric.
same:- It validates two input fields value must be same.

Laravel 8 Ajax Form Validation

In this step by step tutorial I’ll demonstrate you how perform server side validations using ajax in laravel form submit. We will also create example to submit and validate form data using ajax in laravel 8. Please follow the instruction given below:

Step 1: Add Route

In this step we will define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Step 2: Create Controller

Next, we have to create a controller to display form and to handle form validation and submit operations. Lets Create a controller named HomeController using command given below –

Open the HomeController.php file and put the following code in it.

app/Http/Controllers/HomeController.php

In the controller we have following method to display form and to submit and validate form data using ajax:

1)myform()

2)myformPost()

app/Http/Controllers/HomeController.php

Step 3: Create View File

Now, create form blade view file to display form and submit to database. So, Go to resources/views and create resources/views/myform.blade.php and update the following code into it:

resources/views/myform.blade.php

Step 4: 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 Mail using Queue Example

In this tutorial Laravel 8 Send Mail using Queue Example I will show you how to send mail using queue in laravel 8. In this tutorial you will learn to send mails using queue job In laravel application. In Laravel 8 you can use mailtrap, smtp, Mailgun, Postmark, Amazon SES, and sendmail for sending emails.

When we want to send mails in bulk it takes enough time to send all mails. Sending mails in bulk result time consuming process and may not guarantee all mails being sent. In laravel we can use queue job for sending mail in bulk. In this example we will create a queue job with database driver for sending emails.

Laravel 8 Send Mail using Queue Example

In this step by step tutorial I will demonstrate you how to send mail using queue job In laravel application. Please follow instructions given below:

  • Step 1 – Install Laravel 8 App
  • Step 2 – Configuration SMTP & Database
  • Step 3 – Create Mailable Class
  • Step 4 – Add Email Send Route
  • Step 5 – Create Directory And Mail Blade View
  • Step 6 – Configuration Mail Queue
  • Step 7 – Build Queue Job For Sending Mail
  • Step 8 – 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 & Database

In this step we will be setting smtp detail for mail configuration in Laravel, We will be using following Gmail SMTP details such as username, password inside the .envfile.

Step 3 – Create Mailable Class

The mailable class in Laravel used for sending emails. Now, lets create a new laravel mailable class using following artisan command:

Once the above command executed, it will create a new laravel mailable class NotifyMail.php in app/Mail/ directory. Open the NotifyMail.php file and put the following code in it.


Now add email template name in build class of the above created notifymail class.


In next step, Create email template named demoMail.blade.php inside resources/views/emails directory. That’s why 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 Mail Blade View

In this step, create directory name emails inside resources/views directory. Then create an demoMail.blade.php blade view file inside resources/views/emails directory. And update the following code into it:

Step 6 – Configuration Mail Queue

Then open the terminal and run following command for queue database tables:

Next, migrate tables into database:

Step 7 – Build Queue Job For Sending Mail

Now we will create a queue job to send emails using following artisan command:

Then open SendEmailJob.php file which is placed on “app/Jobs” directory. And update the following mail queue code into it:

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 8 Queue Job Tutorial Example

n this Laravel 8 Queue Job Tutorial Example I will show you how to configure, create and use queue jobs in laravel 8 application. In this tutorial you will learn to about creating queue jobs and its application in laravel. In this article I will share example to configure queued jobs for various time consuming tasks in laravel. For example when we want to send mails in bulk it takes enough time to send all mails. Sending mails in bulk result time consuming process and may not guarantee all mails being sent. In laravel we can create a queue job for sending mail in bulk.

Laravel Queue Job

Laravel queue job allows you to process time intensive tasks in background. It results in faster speed and better user experience. With laravel queue jobs you can delay a time-consuming task until a later time. Delaying the time consuming task will speeds up web requests and improve the performance of the applications. For example sending email in bulk is a time-consuming task. We can create a queue job to send email to the user in the background. Laravel queues provide a unified API across various queue backends, such as Amazon SQS, Beanstalk, Redis, or even a relational database.

Laravel 8 Queue Job Tutorial Example

In this step by step tutorial I will demonstrate you with example how to create and use laravel queue jobs in laravel. In this tutorial I will share a simple example to create a queue job with database driver to send email. Please follow the steps given below:

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 & Database

In this step we will be setting smtp detail for mail configuration in Laravel, We will be using following Gmail SMTP details such as username, password inside the .env file.

Step 3 – Create Mailable Class

Now, lets create a new laravel mailable class using following artisan command:

Open SendEmailTest.php file and copy bellow code and post on that file.

app/Mail/SendEmailTest.php

Ok, now create email view using blade file.

resources/views/emails/test.blade.php

Step 4: Configuration of Queue

After that generate migration and create tables for queue. So run command for queue database tables:

Create Migration:

Run Migration:

Step 5: Create Queue Job for Sending Email

Now we will create a queue job to send emails using following artisan command:

Now, open SendEmailJob.php file and put bellow code on that file.

app/Jobs/SendEmailJob.php

Step 6: Create Route for Queue Job

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

process  laravel queue command.

let see the code changes we made

See configuration.

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 Custom Flash Message Tutorial Example

In this Laravel 8 Custom Flash Message Tutorial Example I will show you how to create and use flash message in laravel 8 application. In this tutorial you will learn to create and use flash message in laravel. While working with laravel application we come to situations where we want to show/display various flash success and error messages. In this article I will share various example to create and use various types of flash messages in laravel 8.

Laravel 8 Custom Flash Message Tutorial Example

In this Laravel 8 Custom Flash Message Tutorial you will learn how to create Custom Flash Message in laravel .In this step by step tutorial you will understand how to customize Flash message with example. Please follow the instruction given below:

Step 1: Create Global File For Flash Message

In this step we will create a global flash message file and add following alert in it:

1)success
2)error
3)warning
4)info
5)validation error
Let’s create flash-message.blade.php file and put bellow code on that file.

resources/views/flash-message.blade.php

Step 2: Use Flash Message in Theme

Now we will include global flash message file in app layout as following:

resources/views/layouts/app.blade.php

Step 3: Use Flash Messages with Redirect

we will learn here how to show message when you redirect one by one:

1. Redirect with success message

Below is example to simple redirect with success flash message.

2. Redirect with error message

Below is example to simple redirect with error flash message.

3. Redirect with warning message

Below is example to simple redirect with warning flash message.

4. Redirect with info message

Below is example to simple redirect with info flash message.

5. Validation Error

Laravel 8 Change Date Format Examples

In this Laravel 8 Change Date Format Examples tutorial I will show you how to change date format in laravel. In this tutorial you will learn to change date format using laravel format function in laravel. In this article I will share various example to change date format in laravel. We will also use carbon createFromFormat function to change date format in laravel.

Laravel 8 Change Date Format Examples

In this step by step tutorial I will demonstrate you with example how to change date format in laravel using laravel format function or using carbon createFromFormat function. Please follow the instruction given below:

Laravel 8 Change Date Format Examples

Here, I am sharing various example to change date format using laravel format function:

Example 1:-

Output:-

Example 2:-

Output:-

Example 3 :- Laravel change date format to Y-m-d to m/d/Y

output:-

Example 4:- Laravel change date format to m/d/Y to Y-m-d

output:-

Example 5:- Laravel change date to Y-m-d to d/m/Y

output:-

Laravel 8 Inertia JS CRUD with Jetstream & Tailwind CSS

In this Laravel 8 Inertia JS CRUD with Jetstream Tailwind CSS tutorial I will show you how to create simple authentication and crud application with using Inertia JS with jetstream and tailwind css In laravel. In this tutorial you will learn what is Laravel 8 Inertia JS CRUD with Jetstream & Tailwind CSS. In this tutorial you will learn to create basic CRUD application with authentication using Inertia JS with jetstream package in laravel.

Laravel 8 Livewire CRUD with Jetstream Tailwind CSS

In this step by step guide, we will be creating a simple crud operation application with validation using JetStream with Inertia JS package in laravel 8. In this example you will learn how to insert, read, update and delete data from database using JetStream with Inertia JS package in laravel 8. Please follow the instruction given below:

Step 1: Install Laravel 8

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

Step 2: Create Auth with Jetstream Inertia JS

In this step, we will install Inertia Js and Jetstream Package via the composer dependency manager. Use the following command to install Inertia Js and Jetstream Package.

Now, lets create authentication using Inertia Js. Please use the following command:

Install node js package:

Run package:

Run the migration command to create database table:

Step 3: Create Migration and Model

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

Migration:

Create Post model using following command:

App/Models/Post.php

Step 4: Create Route

Now, 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

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

app/Http/Controllers/PostController.php

Step 6: Share Inertia Var

In this step we will share ‘message’ and ‘errors’ variable for success message and validation error. Lets share this variables on appservices provider as following:

app/Providers/AppServiceProvider.php

Step 7: Create Vue Page

Now, create posts page vue file to list posts and create and update. so, let’s create it and add bellow code on it.

resources/js/Pages/posts.vue

Run npm watch command

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 Autocomplete Search from Database Example

In this Laravel 8 Autocomplete Search from Database Example tutorial, I will show you how to create a dynamic database driven ajax jquery autocomplete search in Laravel. In this tutorial we will create a dynamic search autocomplete which will fetch options from database table using bootstrap jquery.

Laravel 8 Autocomplete Search from Database Example

In this step by step tutorial I will demonstrate you with example creating a autocomplete search input which will fetch options from database. Please follow the instruction given below:

Step 1 : Download and Laravel 8

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

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 2: Create Migration and Model

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

Once above command is executed there will be a migration file created inside database/migrations/ directory, just open migration file and update the function up() method as following:

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

Create model file “app/Models/Item.php” and put following code in item.php file:

app/Models/Item.php

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: Create Controller

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

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

app/Http/Controllers/SearchController.php

Step 5: Create View File

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

resources/views/search.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 8 Ajax Image Upload Example

In this Laravel 8 Ajax Image Upload Example tutorial I will show you how to upload image using ajax in laravel application. In this tutorial you will learn to upload image file using ajax in laravel 8. You will also learn to upload and save image in laravel 8 using ajax. In this laravel ajax image upload example, I’ll show you how to validate upload image into folder and then save it into database using ajax. Before saving image into database we will validate image and then save it into directory using. Before uploading the image we will perform server side validation. After successfully image upload into the database and folder we will display uploaded image on the screen.

Laravel 8 Ajax Image Upload Example

In this step by step tutorial I will demonstrate you with an example to upload image using ajax in laravel application. Please follow the instruction given below:

Step 1 : Install 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 : Database Configuration

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

Step 3: Create ajax_images Table and Model

Now, we have to define table schema for ajax_image table. Open terminal and let’s run the following command to generate a migration along with model file to create ajax_image 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 –

Now, run following command to migrate database schema.

After creating “ajax_images” table ,create AjaxImage model for Categories.

Now, in this step we will create model file  app/Models/AjaxImage.php and put the following code in item.php file:

app/Models/AjaxImage.php

Step 4: Create Route

Now, 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

Now create new controller as AjaxImageUploadController in this path app/Http/Controllers/AjaxImageUploadController.php.

app/Http/Controllers/AjaxImageUploadController.php

Step 6: Create View

Now create ajaxImageUpload.blade.php(resources/views/ajaxImageUpload.blade.php) for layout design code and form for ajax image upload.

resources/views/ajaxImageUpload.blade.php

Note:- Do remember to create “images” folder in public directory.

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 Get Last Executed Query in Laravel 8

In this How to Get Last Executed Query in Laravel 8 tutorial I will show you how to get or print last executed query in laravel application. In this tutorial you will learn to display or print last executed query in your laravel 8 application. In this article I will share various example to display or print last executed sql query in laravel. In this examples we will be using toSql(), DB::enableQueryLog() and DB::getQueryLog() to output of print sql query. These examples we will help us to debug last executed sql query in laravel.

How to Get Last Executed Query in Laravel 8

In this step by step tutorial I will demonstrate you with example to print or output last executed query in laravel application. We will use sql query in laravel 8 using toSql(), DB::enableQueryLog() and DB::getQueryLog(). Please follow the instruction given below:

Example 1:-

Please put the following code in your controller code:

Output:-

Example 2:-

Please put the following code in your controller code:

Output:-

Example 3:-

Please put the following code in your controller code:

Output:-

Laravel 8 Get Current Logged in User Data Example

In this Laravel 8 Get Current Logged in User Data Example tutorial I will show you how to get current logged in user information in laravel 8 application. In this tutorial you will learn to get current logged in user data in laravel. In this article I will share various example to get user data like name, id, email, address etc. I will also show you how to get user data in laravel controller or blade files. We will use auth() helper and Auth facade class to get current user data.

Laravel 8 Get Current Logged in User Data Example

In this step by step tutorial I will demonstrate you how to get current logged in user data using auth helper or Auth facade.

Laravel Get Current User Data Using Auth Helper:

In this example I will show you how to get current user data using auth() helper:

Output:-

Laravel Get Current User Data Using Auth Facade:

In this example I will show you how to get current user data using auth() facade:

Output:-

Laravel 8 Multiple Database Connection Example

In this Laravel 8 Multiple Database Connection Example tutorial I will show you how to connect or use multiple database in single laravel project. In this tutorial you will learn to connect or use multiple database in single laravel project. Sometimes we may need to connect multiple databases in our Laravel application. In this article I will share with you how to connect single or multiple databases using a laravel application.

Laravel 8 Multiple Database Connection Example

In this step by step tutorial I will demonstrate you how to connect multiple database in one laravel application. Please follow the instruction given below:

Set ENV Variable:-


Use ENV Variable:-

Open database.php file and add new connections key as shown

config/database.php

How to use Multiple Database Connection

In this example I will show you how to use multiple database connection in your laravel application:

Laravel Database Connection In migration

Default:-

You can modify above snippet with connection specific as following:

Laravel Database Connection In model

Default:

You can specify database connection in model file as following:

Laravel Database Connection In Controller

Default:

You can specify database connection in controller file as following:

Laravel Database Connection In Query Builder

Default:


You can specify database connection in query as following: