Skip to content

Commit ad4fffe

Browse files
Merge pull request #41 from laravel-shift/softdeletes-bug
Fix multiple SoftDeletes trait
2 parents 0751a26 + 04937e9 commit ad4fffe

File tree

6 files changed

+43
-4
lines changed

6 files changed

+43
-4
lines changed

src/Generators/ModelGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private function addTraits(Model $model, $stub)
192192
}
193193

194194
$stub = str_replace('use Illuminate\\Database\\Eloquent\\Model;', 'use Illuminate\\Database\\Eloquent\\Model;' . PHP_EOL . 'use Illuminate\\Database\\Eloquent\\SoftDeletes;', $stub);
195-
$stub = str_replace('{', '{' . PHP_EOL . ' use SoftDeletes;' . PHP_EOL, $stub);
195+
$stub = preg_replace('/^\\{$/m', '{' . PHP_EOL . ' use SoftDeletes;' . PHP_EOL, $stub);
196196

197197
return $stub;
198198
}

tests/Feature/Generator/ModelGeneratorTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function output_writes_migration_for_model_tree($definition, $path, $mode
5353
->with('stubs/model/class.stub')
5454
->andReturn(file_get_contents('stubs/model/class.stub'));
5555

56+
// TODO: remove conditional expectations
5657
if ($iteration === 0) {
5758
$this->files->expects('get')
5859
->with('stubs/model/fillable.stub')
@@ -67,7 +68,7 @@ public function output_writes_migration_for_model_tree($definition, $path, $mode
6768
->andReturn(file_get_contents('stubs/model/dates.stub'));
6869
}
6970

70-
if ($definition === 'definitions/relationships.bp') {
71+
if ($definition === 'definitions/soft-deletes.bp') {
7172
$this->files->expects('get')
7273
->with('stubs/model/method.stub')
7374
->andReturn(file_get_contents('stubs/model/method.stub'));
@@ -88,8 +89,8 @@ public function modelTreeDataProvider()
8889
{
8990
return [
9091
['definitions/readme-example.bp', 'app/Post.php', 'models/readme-example.php'],
92+
['definitions/with-timezones.bp', 'app/Comment.php', 'models/comment.php'],
9193
['definitions/soft-deletes.bp', 'app/Comment.php', 'models/soft-deletes.php'],
92-
['definitions/with-timezones.bp', 'app/Comment.php', 'models/soft-deletes.php'],
9394
['definitions/relationships.bp', 'app/Comment.php', 'models/relationships.php'],
9495
];
9596
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
models:
22
Comment:
3+
post_id: id
34
softdeletes

tests/fixtures/migrations/soft-deletes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function up()
1515
{
1616
Schema::create('comments', function (Blueprint $table) {
1717
$table->increments('id');
18+
$table->unsignedBigInteger('post_id');
1819
$table->softDeletes();
1920
$table->timestamps();
2021
});

tests/fixtures/models/comment.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
7+
8+
class Comment extends Model
9+
{
10+
use SoftDeletes;
11+
12+
/**
13+
* The attributes that are mass assignable.
14+
*
15+
* @var array
16+
*/
17+
protected $fillable = [];
18+
19+
/**
20+
* The attributes that should be cast to native types.
21+
*
22+
* @var array
23+
*/
24+
protected $casts = [
25+
'id' => 'integer',
26+
];
27+
}

tests/fixtures/models/soft-deletes.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class Comment extends Model
1414
*
1515
* @var array
1616
*/
17-
protected $fillable = [];
17+
protected $fillable = [
18+
'post_id',
19+
];
1820

1921
/**
2022
* The attributes that should be cast to native types.
@@ -23,5 +25,12 @@ class Comment extends Model
2325
*/
2426
protected $casts = [
2527
'id' => 'integer',
28+
'post_id' => 'integer',
2629
];
30+
31+
32+
public function post()
33+
{
34+
return $this->belongsTo(\App\Post::class);
35+
}
2736
}

0 commit comments

Comments
 (0)