Repositories

Creation of a repository

Repositories are nothing more than a class containing all the queries belonging to a model. Each method executes a single query and returns the result/s.

Each repository must have an interface.

class ThreadRepository implements ThreadRepositoryInterface
{
    public function storeThread(string $name, string $status, string|null $description, string $color, int $channelId)
    {
        $thread = Thread::create([
            'name' => $name,
            'status' => $status,
            'description' => $description,
            'color' => $color,
            'channel_id' => $channelId
        ]);
        return $thread;
    }

    public function updateThread(Thread $thread, string $name, string $status, string|null $description, string $color, int $channelId)
    {
        $thread->update([
            'name' => $name,
            'status' => $status,
            'description' => $description,
            'color' => $color,
            'channel_id' => $channelId
        ]);
        return $thread;
    }

    public function destroyThread(Thread $thread)
    {
        $thread->delete();
    }
}

And this is the interface.

interface ThreadRepositoryInterface
{
    public function storeThread(string $name, string $status, string|null $description, string $color, int $channelId);

    public function updateThread(Thread $thread, string $name, string $status, string|null $description, string $color, int $channelId);

    public function destroyThread(Thread $thread);
}

Last updated