Laravel 7 Datatables with Relationship Example

In this tutorial you will learn about the Laravel 7 Datatables with Relationship Example and its application with practical example.

In this Laravel dataTables with relationship example tutorial I’ll show you how to display and filter column in yajra dataTables with relationship.

Laravel 7 Datatables with Relationship Example

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.

In this tutorial I’ll  demonstrates you to how to use a filter on dataTables columns with relationships.

  • Download Laravel Fresh App
  • Setup Database Configuration
  • Install Yajra DataTable
  • Create Migration and Modal
  • Create route
  • Generate Controller by Command
  • Create Blade File
  • Run Development Server

Install Laravel 7/6

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


Run Migration

Now, we need to create migration of tables using following command:

Install Yajra Datatable Package

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.


After set providers and aliases then publish vendor run by the following command.

Create Migration and Modal

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

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


Now run the following command

After you have to put bellow code in your Post model file for create posts table.Now you need to open app/Post.php model and create relationship in this model as following:


Create route

Now, open your “routes/web.php” file and add the following route in it:


Generate Controller by Command

Now, create a new controller PostController using following artisan command:

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.


Create Blade File

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.


Start Development Server

Let’s start the development server using following artisan command:

Now, visit the following link in browser to see the output:

In this tutorial we have learn about the Laravel 7 Datatables with Relationship Example and its application with practical example. I hope you will like this tutorial.