# Models

Krayin uses Laravel's Eloquent ORM (opens new window), with one extra layer on top: the konekt/concord (opens new window) module system. Every Krayin model is built from three pieces so other packages can override it without touching your code:

  • Contract โ€” an interface that declares the public shape of the model.
  • Model โ€” the Eloquent class itself.
  • Proxy โ€” the binding Concord resolves at runtime, so callers ask for the contract and get the implementation registered for it.

This page picks up from Migrations โ€” you've created the examples table and now you need the model that maps to it.

# ๐Ÿ“ Create the model files

There are two ways to scaffold the three pieces. Pick the path you used in Getting Started.

A single command creates all three files in the right place:

php artisan package:make-model Example Webkul/Example

Files created:

File Purpose
packages/Webkul/Example/src/Contracts/Example.php Contract interface
packages/Webkul/Example/src/Models/Example.php Eloquent model
packages/Webkul/Example/src/Models/ExampleProxy.php Concord proxy

Skip ahead to Edit the model body below.

# Option B โ€” Manual setup

Create each file by hand.

# 1. Contract

Create packages/Webkul/Example/src/Contracts/Example.php:

packages
โ””โ”€โ”€ Webkul
    โ””โ”€โ”€ Example
        โ””โ”€โ”€ src
            โ””โ”€โ”€ Contracts
                โ””โ”€โ”€ Example.php
<?php

namespace Webkul\Example\Contracts;

interface Example
{
}

The contract is intentionally empty โ€” you can declare methods on it later if other packages need to call them through the interface.

# 2. Proxy

Create packages/Webkul/Example/src/Models/ExampleProxy.php:

packages
โ””โ”€โ”€ Webkul
    โ””โ”€โ”€ Example
        โ””โ”€โ”€ src
            โ”œโ”€โ”€ ...
            โ””โ”€โ”€ Models
                โ””โ”€โ”€ ExampleProxy.php
<?php

namespace Webkul\Example\Models;

use Konekt\Concord\Proxies\ModelProxy;

class ExampleProxy extends ModelProxy
{
}

The proxy is the indirection layer Concord uses so other packages can swap your model out without rewriting callers.

# 3. Model

Scaffold the Eloquent model with Laravel's generator, then move it into the package:

php artisan make:model Example

Move app/Models/Example.php into packages/Webkul/Example/src/Models/:

packages
โ””โ”€โ”€ Webkul
    โ””โ”€โ”€ Example
        โ””โ”€โ”€ src
            โ”œโ”€โ”€ ...
            โ”œโ”€โ”€ Contracts
            โ”‚   โ””โ”€โ”€ Example.php
            โ””โ”€โ”€ Models
                โ”œโ”€โ”€ Example.php
                โ””โ”€โ”€ ExampleProxy.php

# ๐Ÿงฑ Edit the model body

Open packages/Webkul/Example/src/Models/Example.php and replace its body with the schema from your migration:

<?php

namespace Webkul\Example\Models;

use Illuminate\Database\Eloquent\Model;
use Webkul\Example\Contracts\Example as ExampleContract;

class Example extends Model implements ExampleContract
{
    protected $fillable = [
        'parent_id',
        'title',
        'description',
        'status',
    ];
}

The model implements ExampleContract so Concord can bind the interface to this class. The $fillable array lists the columns Eloquent will accept on create() / update() calls โ€” keep it in sync with the migration.

# โš™๏ธ Register the model with Concord

Concord needs to know about the model so callers can resolve it through the contract. This is done in a dedicated ModuleServiceProvider.

# 1. Create ModuleServiceProvider.php

Add the file alongside your main service provider:

packages
โ””โ”€โ”€ Webkul
    โ””โ”€โ”€ Example
        โ””โ”€โ”€ src
            โ””โ”€โ”€ Providers
                โ”œโ”€โ”€ ExampleServiceProvider.php
                โ””โ”€โ”€ ModuleServiceProvider.php
<?php

namespace Webkul\Example\Providers;

use Konekt\Concord\BaseModuleServiceProvider;

class ModuleServiceProvider extends BaseModuleServiceProvider
{
    protected $models = [
        \Webkul\Example\Models\Example::class,
    ];
}

# 2. Register it in config/concord.php

Add the provider to the modules array in the root config/concord.php:

<?php

return [
    'modules' => [
        // ...
        \Webkul\Example\Providers\ModuleServiceProvider::class,
    ],
];

Concord will now bind Webkul\Example\Contracts\Example โ†’ Webkul\Example\Models\Example automatically, and the ExampleProxy will resolve to the same implementation.

# ๐Ÿงช Verify

Drop into Tinker and resolve the model through the contract:

php artisan tinker
app(\Webkul\Example\Contracts\Example::class);
// => Webkul\Example\Models\Example {...}

If you get an unresolved-binding error, re-check the entry in config/concord.php and run php artisan optimize:clear.

# ๐Ÿ“ Next steps

  • Repository โ€” build the repository layer that controllers use to query and persist your model.
  • Validation โ€” add form-request rules for the fields in $fillable.