Skip to content

Commit

Permalink
Fix laravel reserved queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Anže Časar committed Oct 23, 2018
1 parent 66f4f3b commit c2aea31
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ public function translationModel()
*/
public function translatableAttributes()
{
if(!isset(static::$i18nAttributes[$this->getTable()])) {
return [];
}

return static::$i18nAttributes[$this->getTable()];
}

Expand Down
8 changes: 5 additions & 3 deletions src/TranslatableScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ public function apply(EloquentBuilder $builder, Eloquent $model)
$this->i18nTable = $model->getI18nTable();
$this->fallback = $model->getFallbackLocale();

$this->createJoin($builder, $model);
$this->createWhere($builder, $model);
$this->createSelect($builder, $model);
if(!starts_with($this->table, 'laravel_reserved_')) {
$this->createJoin($builder, $model);
$this->createWhere($builder, $model);
$this->createSelect($builder, $model);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function setUp()

$this->schema()->create('tags', function (Blueprint $table) {
$table->increments('id');
$table->integer('parent_id')->unsigned()->nullable();
$table->timestamps();
});

Expand Down
12 changes: 12 additions & 0 deletions tests/TestCRUD.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,16 @@ public function testIncrementDecrement()
$user->decrement('age');
$this->assertEquals(20, User::first()->age);
}

public function testHasQuery()
{
$tag1 = Tag::forceCreate(['title' => 'Tag1']);
$tag2 = Tag::forceCreate(['title' => 'Tag2']);
$tag3 = Tag::forceCreate(['title' => 'Tag2']);

$tag3->parent_id = $tag1->id;
$tag3->save();

$this->assertCount(1, Tag::has('children')->get());
}
}
5 changes: 5 additions & 0 deletions tests/stubs/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public function posts()
{
return $this->belongsToMany(Post::class);
}

public function children()
{
return $this->hasMany(static::class, 'parent_id', 'id');
}
}

0 comments on commit c2aea31

Please sign in to comment.