Laravel 8 Datatables Filter Column Relationship Tutorial

In this tutorial you will learn about the Laravel 8 Datatables Filter Column Relationship Tutorial and its application with practical example.

In this Laravel 8 Datatables Filter Column Relationship Tutorial I will show you how to display and filter column in yajra dataTables with relationship in laravel 8. In this tutorial you will learn to display and filter column in yajra dataTables with relationship. In this article I will share example to display and filter column in yajra dataTables with relationship in laravel.

Let suppose you have two database tables first is posts and the other one is users and you have relationship in each table. Now you want to display and filter posts title and who write these posts (author name). This would require you to use laravel yajra dataTables columns with a relationship.

Laravel 8 Datatables Filter Column Relationship Tutorial

In this step by step tutorial I’ll demonstrates you to how to use a filter on dataTables columns with relationships. Please follow the instruction given below:

  • Step 1 – Install Laravel 8 App
  • Step 2 – Connecting App to Database
  • Step 3 – Install Yajra DataTable
  • Step 4 – Create Migration and Modal
  • Step 5 – Add route
  • Step 6 – Create Controller by Artisan Command
  • Step 7 – Create Blade File
  • 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 – 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 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 4 – Create Migration and Modal

Now let’s create post table migration and create Post Modal using following artisan command:

his will create migration, open migration file and put the following code in it:

database/migrations/2020_05_20_070104_create_posts_table.php


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


Now update following code in your Post model file to create a posts table.

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 by Artisan Command

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

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

app/Http/Controllers/PostController.php

 

Step 7 – Create Blade File

In this step we will create blade file. Go to resources/views directory and create new file users.blade.php. After that, put the following html and javascript code into your blade view file:

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 –

In this tutorial we have learn about the Laravel 8 Datatables Filter Column Relationship Tutorial and its application with practical example. I hope you will like this tutorial.