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
4 changes: 2 additions & 2 deletions src/Lexers/ModelLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ private function buildModel(string $name, array $columns)
$model->addColumn($column);

$foreign = collect($column->modifiers())->filter(function ($modifier) {
return (is_array($modifier) && key($modifier) === 'foreign') || $modifier === 'foreign';
return collect($modifier)->containsStrict('foreign') || collect($modifier)->has('foreign');
})->flatten()->first();

if ($column->name() !== 'id' && (in_array($column->dataType(), ['id', 'uuid']) || $foreign)) {
if (($column->name() !== 'id') && ($column->dataType() === 'id') || $foreign) {
$reference = $column->name();

if ($foreign && $foreign !== 'foreign') {
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Generators/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ public function modelTreeDataProvider()
['drafts/model-key-constraints.yaml', 'database/migrations/timestamp_create_orders_table.php', 'migrations/model-key-constraints.php'],
['drafts/disable-auto-columns.yaml', 'database/migrations/timestamp_create_states_table.php', 'migrations/disable-auto-columns.php'],
['drafts/uuid-shorthand.yaml', 'database/migrations/timestamp_create_people_table.php', 'migrations/uuid-shorthand.php'],
['drafts/uuid-shorthand-invalid-relationship.yaml', 'database/migrations/timestamp_create_age_cohorts_table.php', 'migrations/uuid-shorthand-invalid-relationship.php'],
['drafts/unconventional-foreign-key.yaml', 'database/migrations/timestamp_create_states_table.php', 'migrations/unconventional-foreign-key.php'],
['drafts/resource-statements.yaml', 'database/migrations/timestamp_create_users_table.php', 'migrations/resource-statements.php'],
['drafts/enum-options.yaml', 'database/migrations/timestamp_create_messages_table.php', 'migrations/enum-options.php'],
Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/Generators/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ public function modelTreeDataProvider()
['drafts/resource-statements.yaml', 'app/User.php', 'models/resource-statements.php'],
['drafts/all-column-types.yaml', 'app/AllType.php', 'models/all-column-types.php'],
['drafts/alias-relationships.yaml', 'app/Salesman.php', 'models/alias-relationships.php'],
['drafts/uuid-shorthand-invalid-relationship.yaml', 'app/AgeCohort.php', 'models/uuid-shorthand-invalid-relationship.php'],
];
}

Expand All @@ -687,6 +688,7 @@ public function laravel8ModelTreeDataProvider()
['drafts/resource-statements.yaml', 'app/User.php', 'models/resource-statements-laravel8.php'],
['drafts/all-column-types.yaml', 'app/AllType.php', 'models/all-column-types-laravel8.php'],
['drafts/alias-relationships.yaml', 'app/Salesman.php', 'models/alias-relationships-laravel8.php'],
['drafts/uuid-shorthand-invalid-relationship.yaml', 'app/AgeCohort.php', 'models/uuid-shorthand-invalid-relationship-laravel8.php'],
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
models:
AgeCohort:
id
timestamps
name: string:100
description: string:500 nullable
min_age: integer nullable
max_age: integer nullable
uuid: uuid unique
36 changes: 36 additions & 0 deletions tests/fixtures/migrations/uuid-shorthand-invalid-relationship.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

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

class CreateAgeCohortsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('age_cohorts', function (Blueprint $table) {
$table->id();
$table->string('name', 100);
$table->string('description', 500)->nullable();
$table->integer('min_age')->nullable();
$table->integer('max_age')->nullable();
$table->uuid('uuid')->unique();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('age_cohorts');
}
}
6 changes: 0 additions & 6 deletions tests/fixtures/models/all-column-types-laravel8.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,4 @@ class AllType extends Model
'unsignedSmallInteger' => 'integer',
'unsignedTinyInteger' => 'integer',
];


public function uuid()
{
return $this->belongsTo(\App\Uuid::class);
}
}
6 changes: 0 additions & 6 deletions tests/fixtures/models/all-column-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,4 @@ class AllType extends Model
'timestamp',
'timestampTz',
];


public function uuid()
{
return $this->belongsTo(\App\Uuid::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class AgeCohort extends Model
{
use HasFactory;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'description',
'min_age',
'max_age',
'uuid',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
];
}
30 changes: 30 additions & 0 deletions tests/fixtures/models/uuid-shorthand-invalid-relationship.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

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