Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ protected function buildPivotTableDefinition(array $segments)
$definition = '';

foreach ($segments as $segment) {
$column = Str::lower($segment);
$column = Str::before(Str::lower($segment), ':');
$references = 'id';
$on = Str::plural($column);
$foreign = Str::singular($column) . '_' . $references;
Expand Down Expand Up @@ -276,10 +276,22 @@ protected function getPivotClassName(array $segments)

protected function getPivotTableName(array $segments)
{
$isCustom = collect($segments)
->filter(function ($segment) {
return Str::contains($segment, ':');
})->first();

if ($isCustom) {
$table = Str::after($isCustom, ':');

return $table;
}

$segments = array_map(function ($name) {
return Str::snake($name);
}, $segments);
sort($segments);

return strtolower(implode('_', $segments));
}

Expand Down
13 changes: 8 additions & 5 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,24 @@ private function buildRelationships(Model $model)
}
}

$class = Str::studly($class ?? $method_name);
$class_name = Str::studly($class ?? $method_name);

if ($type === 'morphTo') {
$relationship = sprintf('$this->%s()', $type);
} elseif ($type === 'morphMany' || $type === 'morphOne') {
$relation = Str::lower(Str::singular($column_name)) . 'able';
$relationship = sprintf('$this->%s(%s::class, \'%s\')', $type, '\\' . $model->fullyQualifiedNamespace() . '\\' . $class, $relation);
$relationship = sprintf('$this->%s(%s::class, \'%s\')', $type, '\\' . $model->fullyQualifiedNamespace() . '\\' . $class_name, $relation);
} elseif (!is_null($key)) {
$relationship = sprintf('$this->%s(%s::class, \'%s\', \'%s\')', $type, '\\' . $model->fullyQualifiedNamespace() . '\\' . $class, $column_name, $key);
$relationship = sprintf('$this->%s(%s::class, \'%s\', \'%s\')', $type, '\\' . $model->fullyQualifiedNamespace() . '\\' . $class_name, $column_name, $key);
} elseif (!is_null($class) && $type === 'belongsToMany') {
$relationship = sprintf('$this->%s(%s::class, \'%s\')', $type, '\\' . $model->fullyQualifiedNamespace() . '\\' . $class_name, $column_name);
$column_name = $class;
} else {
$relationship = sprintf('$this->%s(%s::class)', $type, '\\' . $model->fullyQualifiedNamespace() . '\\' . $class);
$relationship = sprintf('$this->%s(%s::class)', $type, '\\' . $model->fullyQualifiedNamespace() . '\\' . $class_name);
}

if ($type === 'morphTo') {
$method_name = Str::lower($class);
$method_name = Str::lower($class_name);
} elseif (in_array($type, ['hasMany', 'belongsToMany', 'morphMany'])) {
$method_name = Str::plural($column_name);
}
Expand Down
58 changes: 58 additions & 0 deletions tests/Feature/Generator/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,64 @@ public function output_does_not_duplicate_pivot_table_migration_laravel6()
$this->assertEquals(['created' => [$company_migration, $people_migration, $pivot_migration]], $this->subject->output($tree));
}

/**
* @test
*/
public function output_also_creates_pivot_table_migration_with_custom_name()
{
$this->files->expects('stub')
->with('migration.stub')
->andReturn(file_get_contents('stubs/migration.stub'));

$now = Carbon::now();
Carbon::setTestNow($now);

$model_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_users_table.php');
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_test_table.php');

$this->files->expects('put')
->with($model_migration, $this->fixture('migrations/custom-pivot-table-name-user.php'));
$this->files->expects('put')
->with($pivot_migration, $this->fixture('migrations/custom-pivot-table-name-test.php'));

$tokens = $this->blueprint->parse($this->fixture('definitions/custom-pivot-table-name.bp'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['created' => [$model_migration, $pivot_migration]], $this->subject->output($tree));
}

/**
* @test
*/
public function output_also_creates_pivot_table_migration_with_custom_name_laravel6()
{
$app = \Mockery::mock();
$app->shouldReceive('version')
->withNoArgs()
->andReturn('6.0.0');
App::swap($app);

$this->files->expects('stub')
->with('migration.stub')
->andReturn(file_get_contents('stubs/migration.stub'));

$now = Carbon::now();
Carbon::setTestNow($now);

$model_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_users_table.php');
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_test_table.php');

$this->files->expects('put')
->with($model_migration, $this->fixture('migrations/custom-pivot-table-name-user-laravel6.php'));
$this->files->expects('put')
->with($pivot_migration, $this->fixture('migrations/custom-pivot-table-name-test-laravel6.php'));

$tokens = $this->blueprint->parse($this->fixture('definitions/custom-pivot-table-name.bp'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['created' => [$model_migration, $pivot_migration]], $this->subject->output($tree));
}

/**
* @test
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/Feature/Generator/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,39 @@ public function output_generates_models_with_custom_namespace_correctly()
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
}

/**
* @test
*/
public function output_generates_models_with_custom_pivot_columns()
{
$this->files->expects('stub')
->with('model/class.stub')
->andReturn(file_get_contents('stubs/model/class.stub'));
$this->files->expects('stub')
->with('model/fillable.stub')
->andReturn(file_get_contents('stubs/model/fillable.stub'));
$this->files->expects('stub')
->with('model/casts.stub')
->andReturn(file_get_contents('stubs/model/casts.stub'));
$this->files->expects('stub')
->with('model/method.stub')
->andReturn(file_get_contents('stubs/model/method.stub'));
$this->files->expects('stub')
->with('model/hidden.stub')
->andReturn(file_get_contents('stubs/model/hidden.stub'));

$this->files->expects('exists')
->with('app')
->andReturnTrue();
$this->files->expects('put')
->with('app/User.php', $this->fixture('models/custom-pivot-table-name.php'));

$tokens = $this->blueprint->parse($this->fixture('definitions/custom-pivot-table-name.bp'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['created' => ['app/User.php']], $this->subject->output($tree));
}

public function modelTreeDataProvider()
{
return [
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/definitions/custom-pivot-table-name.bp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
models:
User:
name: string
remember_token: remembertoken
relationships:
belongsToMany: Account:test
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

class CreateTestTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('test', function (Blueprint $table) {
$table->unsignedBigInteger('account_id');
$table->unsignedBigInteger('user_id');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('test');
}
}
31 changes: 31 additions & 0 deletions tests/fixtures/migrations/custom-pivot-table-name-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

class CreateTestTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('test', function (Blueprint $table) {
$table->foreignId('account_id');
$table->foreignId('user_id');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('test');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->rememberToken();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
33 changes: 33 additions & 0 deletions tests/fixtures/migrations/custom-pivot-table-name-user.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->rememberToken();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
41 changes: 41 additions & 0 deletions tests/fixtures/models/custom-pivot-table-name.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
];

/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'remember_token',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
];


public function accounts()
{
return $this->belongsToMany(\App\Account::class, 'test');
}
}