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
7 changes: 3 additions & 4 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,15 @@ protected function buildRelationships(Model $model)
if (Str::contains($reference, ':')) {
[$foreign_reference, $column_name] = explode(':', $reference);

$method_name = $is_model_fqn ? Str::afterLast($foreign_reference, '\\') : Str::beforeLast($column_name, '_id');
$method_name = Str::beforeLast($column_name, '_id');

if (Str::contains($foreign_reference, '.')) {
[$class, $key] = explode('.', $foreign_reference);

if ($key === 'id') {
$key = null;
} else {
$method_name = $is_model_fqn ? Str::lower(Str::afterLast($class, '\\')) : Str::lower($class);
}
$method_name = $is_model_fqn ? Str::lower(Str::afterLast($class, '\\')) : Str::lower($class);
} else {
$class = $foreign_reference;
}
Expand All @@ -230,7 +229,7 @@ protected function buildRelationships(Model $model)
if ($type === 'morphTo') {
$relationship = sprintf('$this->%s()', $type);
} elseif ($type === 'morphMany' || $type === 'morphOne') {
$relation = Str::lower(Str::singular($column_name)) . 'able';
$relation = Str::lower($is_model_fqn ? Str::singular(Str::afterLast($column_name, '\\')) : Str::singular($column_name)) . 'able';
$relationship = sprintf('$this->%s(%s::class, \'%s\')', $type, $fqcn, $relation);
} elseif (!is_null($key)) {
$relationship = sprintf('$this->%s(%s::class, \'%s\', \'%s\')', $type, $fqcn, $column_name, $key);
Expand Down
32 changes: 32 additions & 0 deletions tests/Feature/Generators/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,38 @@ public function output_generates_relationships_added_with_full_model_namespace()
$this->assertEquals(['created' => ['app/Recurrency.php']], $this->subject->output($tree));
}

/**
* @test
* @environment-setup useLaravel8
*/
public function output_generates_morphone_morphmany_relation_string_when_using_fqn()
{
$this->files->expects('stub')
->with($this->modelStub)
->andReturn($this->stub($this->modelStub));
$this->files->expects('stub')
->with('model.fillable.stub')
->andReturn($this->stub('model.fillable.stub'));
$this->files->expects('stub')
->with('model.casts.stub')
->andReturn($this->stub('model.casts.stub'));
$this->files->expects('stub')
->with('model.method.stub')
->andReturn($this->stub('model.method.stub'));

$this->files->expects('exists')
->with('app')
->andReturnTrue();

$this->files->expects('put')
->with('app/Flag.php', $this->fixture('models/model-relationships-morphone-morphmany-with-fqn-laravel8.php'));

$tokens = $this->blueprint->parse($this->fixture('drafts/model-relationships-morphone-morphmany-with-fqn.yaml'));
$tree = $this->blueprint->analyze($tokens);

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

/**
* @test
* @environment-setup useLaravel8
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
models:
Flag:
user_id: id
relationships:
morphOne: \Other\Package\Order:stars
morphMany: \Other\Package\Duration, \App\MyCustom\Folder\Transaction:line
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App;

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

class Flag extends Model
{
use HasFactory;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id',
];

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


public function stars()
{
return $this->morphOne(\Other\Package\Order::class, 'starable');
}

public function durations()
{
return $this->morphMany(\Other\Package\Duration::class, 'durationable');
}

public function lines()
{
return $this->morphMany(\App\MyCustom\Folder\Transaction::class, 'lineable');
}

public function user()
{
return $this->belongsTo(\App\User::class);
}
}