Home Laravel 8 Laravel 8 Join Two Tables

Laravel 8 Join Two Tables

6 min read

In this tutorial I am going to teach you how to join two tables in laravel 8.how to make the relationship. Relationship are very important stuff in Mvc Architecture.

We have two tables in the database categories and products.


 Id        Catname

 1         drink   
 2          cake


Id       prodname       category_id

1        milk                 1
2        coffee                1
3        ice cake             2

We have already created the above tables in the database.

In this scenario  make a one to many relationship to join the table.

Example  One category has many products

Example drink is one category it has many drink item, coffee,tea,juice,milk like this

First Step Create the Project

composer create-project --prefer-dist laravel/laravel posex

Create the Controller

php artisan make:controller ProductController --resource

Create a Models


php artisan make:model Category


php artisan make:model Product

Category Model

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Product;

class Category extends Model
    protected $fillable = [

    public function products()
        return $this->hasMany(Product::class, 'category_id', 'id');

Product Model


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Category;

class Product extends Model
    protected $fillable = [

    public function category()
        return $this->belongsTo(Category::class);

Create the Views

Create the folder Products inside the views and create two pages.

  • view.blade.php
  • index.blade.php


<!DOCTYPE html>
    <title>Contact Laravel 8 CRUD</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<div class="container">



<table class="table">
      <th scope="col">#</th>
      <th scope="col">Product name</th>
      <th scope="col">Category Name</th>
             @foreach ($products as $product)
                  <td>{{ $loop->iteration }}</td>
                  <td>{{ $product->prodname }}</td>
                  <td>{{ $product->category->catname }}</td>


Product Controller


namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
    public function index()
        $products = Product::with('category')->get();
        $categories = Category::with('products')->get();
        return view('products.index', compact('products', 'categories'));




use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;

Route::get('/', function () {
    return view('welcome');

Route::get('products', [ProductController::class, 'index']);

i have attached the video link below. which will do this tutorials step by step.


Load More Related Articles
Load More By admin
Load More In Laravel 8

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also

Mastering Object-Oriented Programming Project in Java

In this tutorial, I will teach Java programming projects along with Object-Oriented Progra…