---
title: "Modely z konfigurácie — Package Toolkit for Laravel | Roundly"
description: "Zistite konfigurovateľnú triedu Eloquent modelu cez ModelResolver alebo ResolvesModels — trieda, ktorá nededí z modelu balíka, vyhodí výnimku."
url: https://roundly-consulting.com/sk/open-source/navody/package-toolkit-for-laravel/modely-z-konfiguracie
language: sk
---

[Všetky balíky](https://roundly-consulting.com/sk/open-source.md)

[Package Toolkit for Laravel](https://roundly-consulting.com/sk/open-source/navody/package-toolkit-for-laravel.md)

# Modely z konfigurácie

Balíky umožňujú hostiteľom vymeniť modely cez config('<package>.models.\*'). ModelResolver::for(string $key, ?string $default = null, ?string $base = null) vybaví zistenie aj overenie naraz: predvolená hodnota platí len pre nenastavený kľúč (chýbajúci, null alebo prázdny — '' či len medzery) a trieda musí existovať, byť Eloquent modelom a byť $base alebo jeho podtriedou. $base je predvolene $default — model balíka, ktorý hostiteľ rozširuje — inak Model. Čokoľvek iné vyhodí InvalidConfigurationException s názvom kľúča; nesprávna trieda sa nikdy potichu nenahradí modelom balíka:

```php
use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\PackageToolkit\Support\ModelResolver;

// Resolve + validate a model class from config: absent or blank → the packaged default; anything
// that isn't that class or a subclass of it throws (never a silent fall-back).
$class = ModelResolver::for('comments.models.comment', Comment::class);       // class-string<Comment>
$model = ModelResolver::newModel('comments.models.comment', Comment::class);  // a fresh instance

// The default is only a suggestion here — a host may name any Eloquent model:
$tenant = ModelResolver::for('comments.models.tenant', Team::class, base: Model::class);

// No default: the key is required, and any Eloquent model is accepted.
$class = ModelResolver::for('comments.models.comment');
```

Ak je predvolená hodnota len návrhom, ktorý môže hostiteľ nahradiť nesúvisiacim modelom, odovzdajte base: Model::class. newModel() prijíma rovnaké argumenty a vráti novú inštanciu zistenej triedy. Model hostiteľa, ktorý nededí z modelu balíka — alebo názov neexistujúcej triedy — zlyhá takto:

```php
// config('comments.models.comment') === App\Models\Post::class — not a Comment
ModelResolver::for('comments.models.comment', Comment::class);
// throws InvalidConfigurationException:
// Configuration value [comments.models.comment] must be a class-string of [<Comment's class>], [App\Models\Post] given.

// config('comments.models.comment') === 'App\Nope' — no such class
// throws the same message, with [App\Nope] given.
```

## Trait ResolvesModels

Pre triedy, ktoré z konfigurácie zisťujú viac modelov, obaľuje trait ResolvesModels ModelResolver do dvoch protected metód — modelClass() a newModel() — obe s rovnakými voliteľnými $default a $base a s rovnakými pravidlami:

```php
use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\PackageToolkit\Concerns\ResolvesModels;

final class CommentRepository
{
    use ResolvesModels;

    public function create(array $attributes): Model
    {
        $comment = $this->newModel('comments.models.comment', Comment::class);
        $comment->fill($attributes)->save();

        return $comment;
    }

    public function find(int|string $id): ?Model
    {
        return $this->modelClass('comments.models.comment', Comment::class)::query()->find($id);
    }
}
```

observesModel() v základnom provideri používa ten istý validátor — pozrite Väzby a observery.

## Prejavte lásku k open source

Tento balík je zadarmo pod licenciou MIT. Ak vám šetrí čas, jednorazový príspevok alebo členstvo na Patreone nám pomôže ho ďalej udržiavať, testovať a dokumentovať.

[Ďalšie spôsoby podpory vrátane kryptomien](https://roundly-consulting.com/sk/podporte-nas.md)

Odoslaním daru súhlasíte s našimi [podmienkami prijímania darov](https://roundly-consulting.com/sk/podmienky-darov.md).

[Podporte našu open-source prácu (otvorí sa v novej karte)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Pridajte sa k nám na Patreone (otvorí sa v novej karte)](https://www.patreon.com/cw/roundly)

## Chcete to zabudovať do svojho produktu?

Naše balíky integrujeme do zákazkových Laravel a AI riešení. Napíšte nám, na čom pracujete, a ozveme sa do 48 hodín.

[Ponuka do 48 hodín](https://roundly-consulting.com/sk/kontakt.md) [Prezrieť všetky balíky](https://roundly-consulting.com/sk/open-source/navody/package-toolkit-for-laravel.md)
