peltonsolutions/laravel-pivot-table-helper

A custom package for Laravel to assist with creating BelongsToMany pivot migrations.

0.2.0 2024-03-14 16:03 UTC

This package is auto-updated.

Last update: 2024-05-14 16:39:46 UTC


README

Description WIP

Things to add

  • allow nullable relationships, when you have multiple
  • add morphToMany
  • add command to automatically create the migration file from a model/relationship
  • If the target table already exists, update the table instead
  • Add "down" generation, which determines if the target table only has the two column relationship or not. If it only has these two fields, it will drop the table. If it has more fields, it will remove the column for the relationship column.

Install

You can install the package via composer using the following command:

composer require peltonsolutions/laravel-pivot-table-helper

Usage

<?php

use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Schema;
use PeltonSolutions\LaravelPivotTableHelper\Models\GenerateBelongsToManyMigration;

return new class extends Migration {
	/**
	 * Run the migrations.
	 */
	public function up(): void
	{
		GenerateBelongsToManyMigration::createMigration(
			(new User())->roles()
		);
	}

	/**
	 * Reverse the migrations.
	 */
	public function down(): void
	{
		Schema::dropIfExists((new User())->roles()->getTable());
	}
};

Or, you can pass an array of relationships. This assumes it's sharing the same pivot table, and the same parent class

<?php

use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Schema;
use PeltonSolutions\LaravelPivotTableHelper\Models\GenerateBelongsToManyMigration;

return new class extends Migration {
	/**
	 * Run the migrations.
	 */
	public function up(): void
	{
	    $instance = new User();
		GenerateBelongsToManyMigration::createMigration([
			$instance->relationship1(),
			$instance->relationship2(),
		]);
	}

	/**
	 * Reverse the migrations.
	 */
	public function down(): void
	{
		Schema::dropIfExists((new User())->roles()->getTable());
	}
};

Security

If you discover any security-related issues, please email security@peltonsolutions.com instead of using the issue tracker.

Credits

License

laravel-pivot-table-helper is open-sourced software. It's licensed under the MIT license, which is a permissive license allowing the software to be used, modified, and shared.