Laravel join table products with table categories

Here's an example of migration files for creating the "products" and "categories" tables in Laravel:

Create a migration for the "products" table:

php artisan make:migration create_products_table --create=products

Open the generated migration file (located in the "database/migrations" directory) and update the up() method as follows:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProductsTable extends Migration
{
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('category_id')->unsigned();
            // Add other product columns as needed

            $table->timestamps();

            $table->foreign('category_id')->references('id')->on('categories');
        });
    }

    public function down()
    {
        Schema::dropIfExists('products');
    }
}

Create a migration for the "categories" table:

php artisan make:migration create_categories_table --create=categories

Open the generated migration file (located in the "database/migrations" directory) and update the up() method as follows:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCategoriesTable extends Migration
{
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            // Add other category columns as needed

            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('categories');
    }
}

Once you have created these migration files, you can run the migrations using the following command:

php artisan migrate

This will create the "products" and "categories" tables in your database with the specified columns.

To join the "products" table with the "categories" table in Laravel, you can utilize Laravel's Eloquent ORM and its relationship feature. Here's an example of how you can achieve this:

Define the relationships in your models:

// Product.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    public function category()
    {
        return $this->belongsTo(Category::class);
    }
}

 

// Category.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    public function products()
    {
        return $this->hasMany(Product::class);
    }
}

Retrieve products with their associated category:

 $products = Product::with('category')->get();

foreach ($products as $product) {
    echo $product->name . ' - ' . $product->category->name;
    // You can access other properties of the product and category as well
}

In this example, the with('category') method eager loads the category relationship for each product, reducing the number of queries required to retrieve the category data.

By using this approach, you can easily join the "products" and "categories" tables and access the related category for each product.