Skip to content

Commit

Permalink
Merge pull request #12 from Laravel-Lang/1.x
Browse files Browse the repository at this point in the history
Fixed eager loading
  • Loading branch information
andrey-helldar authored Jun 22, 2024
2 parents 9372b64 + 7315200 commit a492b6d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
22 changes: 18 additions & 4 deletions src/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace LaravelLang\Models;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Arr;
Expand All @@ -21,6 +20,8 @@
use LaravelLang\Models\Services\Relation;

use function app;
use function array_merge;
use function array_unique;
use function filled;
use function in_array;
use function is_iterable;
Expand All @@ -45,11 +46,14 @@ protected static function translationModelName(): string
return static::class . Config::shared()->models->suffix;
}

public function initializeHasTranslations(): void
{
$this->with = array_unique(array_merge($this->with, ['translations']));
}

public function translations(): HasMany
{
return $this->hasMany(static::translationModelName(), 'item_id')->afterQuery(
fn (Collection $items) => $items->keyBy('locale')
);
return $this->hasMany(static::translationModelName(), 'item_id');
}

public function hasTranslated(string $column, Locale|string|null $locale = null): bool
Expand Down Expand Up @@ -150,6 +154,16 @@ public function setAttribute($key, $value): Model
return parent::setAttribute($key, $value);
}

public function setRelation($relation, $value): static
{
$this->relations[$relation] = match ($relation) {
'translations' => $value->keyBy('locale'),
default => $value
};

return $this;
}

public function newInstance($attributes = [], $exists = false): static
{
$basic = Arr::except($attributes, $this->translatable());
Expand Down
1 change: 1 addition & 0 deletions testbench.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
laravel: ./workbench
providers:
- App\Providers\AppServiceProvider
- LaravelLang\Config\ServiceProvider
- LaravelLang\Locales\ServiceProvider
- LaravelLang\Models\ServiceProvider
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/Models/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use App\Models\TestModel;
use App\Models\TestModelTranslation;
use LaravelLang\Models\Exceptions\AttributeIsNotTranslatableException;
use LaravelLang\Models\Exceptions\UnavailableLocaleException;
Expand Down Expand Up @@ -68,6 +69,18 @@
expect($model->getTranslation(FakeValue::ColumnTitle, FakeValue::LocaleCustom))->toBeNull();
});

test('lazy loading', function () {
$model1 = fakeModel(main: 'Foo');
$model2 = fakeModel(main: 'Bar');

TestModel::query()->get()->each(
fn (TestModel $model) => match ($model->getKey()) {
$model1->getKey() => expect($model->title)->toBe('Foo'),
$model2->getKey() => expect($model->title)->toBe('Bar'),
}
);
});

test('non-translatable attribute', function () {
$key = fake()->word;

Expand Down
21 changes: 21 additions & 0 deletions workbench/app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Providers;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
$this->lazyLoading();
}

protected function lazyLoading(): void
{
Model::preventLazyLoading();
}
}
14 changes: 8 additions & 6 deletions workbench/bootstrap/cache/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
19 => 'Illuminate\\Translation\\TranslationServiceProvider',
20 => 'Illuminate\\Validation\\ValidationServiceProvider',
21 => 'Illuminate\\View\\ViewServiceProvider',
22 => 'LaravelLang\\Config\\ServiceProvider',
23 => 'LaravelLang\\Locales\\ServiceProvider',
24 => 'LaravelLang\\Models\\ServiceProvider',
22 => 'App\\Providers\\AppServiceProvider',
23 => 'LaravelLang\\Config\\ServiceProvider',
24 => 'LaravelLang\\Locales\\ServiceProvider',
25 => 'LaravelLang\\Models\\ServiceProvider',
),
'eager' =>
array (
Expand All @@ -39,9 +40,10 @@
7 => 'Illuminate\\Pagination\\PaginationServiceProvider',
8 => 'Illuminate\\Session\\SessionServiceProvider',
9 => 'Illuminate\\View\\ViewServiceProvider',
10 => 'LaravelLang\\Config\\ServiceProvider',
11 => 'LaravelLang\\Locales\\ServiceProvider',
12 => 'LaravelLang\\Models\\ServiceProvider',
10 => 'App\\Providers\\AppServiceProvider',
11 => 'LaravelLang\\Config\\ServiceProvider',
12 => 'LaravelLang\\Locales\\ServiceProvider',
13 => 'LaravelLang\\Models\\ServiceProvider',
),
'deferred' =>
array (
Expand Down

0 comments on commit a492b6d

Please sign in to comment.