LayaPHP: Self-Hosted Text Classification for Laravel | Mohamed Said       [Skip to content](#main)  [ ![](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png) Mohamed SaidLaravel Backend Engineer ](https://www.msaied.com) - [Home](https://www.msaied.com)
- [Projects](https://www.msaied.com/projects)
- [Articles](https://www.msaied.com/articles)
- [Certificates](https://www.msaied.com/certificates)
- [About](https://www.msaied.com#about)

           [  Contact](https://www.msaied.com#contact) Menu 

Menu
----

Close 

 - [HomeStart here](https://www.msaied.com)
- [ProjectsCase studies](https://www.msaied.com/projects)
- [ArticlesEngineering notes](https://www.msaied.com/articles)
- [CertificatesCredentials](https://www.msaied.com/certificates)
- [AboutHow I work](https://www.msaied.com#about)
- [ContactGet in touch](https://www.msaied.com#contact)

  [Start a conversation](https://www.msaied.com#contact) [WhatsApp](https://wa.me/201094619204) [Email](mailto:hello@msaied.com) 

 1. [Home](https://www.msaied.com)
2. /
3. [Articles](https://www.msaied.com/articles)
4. /
5. [Composer Pacakge](https://www.msaied.com/articles?category=composer-pacakge)
6. /
7. LayaPHP: Self-Hosted Text Classification for PHP and Laravel

   [Composer Pacakge](https://www.msaied.com/articles?category=composer-pacakge) [AI](https://www.msaied.com/articles?category=ai) 

 LayaPHP: Self-Hosted Text Classification for PHP and Laravel
=============================================================

 LayaPHP is a PHP client for Laya, an open-source decision model that runs on your own infrastructure. Classify text into labels, yes/no answers, and scored scales without paying per-API call.

 ![](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp) [Mohamed Said](https://www.msaied.com#person) Published 5 Oct 2026 · Updated 6 Oct 2026 · 3 min read

ShareCopy linkCopied

 ![LayaPHP: Self-Hosted Text Classification for PHP and Laravel](https://cdn.msaied.com/744/b8ac362adc9fa9e5721d6b0dadee60f1.png) 

  On this page +1. [What Is LayaPHP?](#what-is-layaphp)
2. [Classifying a Product Review](#classifying-a-product-review)
3. [Other Question Types](#other-question-types)
4. [Installation and Setup](#installation-and-setup)
5. [Testing Without a Running Model](#testing-without-a-running-model)
6. [Key Takeaways](#key-takeaways)

 What Is LayaPHP?
----------------

[Laya](https://github.com/NandhaKishorM/laya) is an open-source, non-autoregressive decision model designed for quick categorical choices and risk checks. Instead of generating conversational text, it answers three kinds of questions about any input string:

- **Yes/No** — returns a probability and a boolean answer at a configurable threshold.
- **Choice** — picks one label from a set you define, with a confidence score.
- **Score** — maps text onto an ordered scale (e.g., "not urgent" → "blocking") and returns a numeric position.

[LayaPHP](https://github.com/marcreichel/laya-php), created by Marc Reichel, is a typed PHP client that sends text to a running `laya-serve` process and returns structured PHP objects for each answer. Because Laya runs on your own infrastructure, you pay only for compute — there is no per-call API charge.

Classifying a Product Review
----------------------------

A practical use case is moderating e-commerce reviews. The `predict()` method batches multiple questions into a single request:

```php
use MarcReichel\Laya\Laya;
use MarcReichel\Laya\Question;

function classifyReview(Laya $laya, string $body): array
{
    $result = $laya->predict($body, [
        'spam'      => Question::yesNo('Is this review spam or an advertisement?'),
        'abuse'     => Question::yesNo('Does this review contain insults, hate speech, or threats?'),
        'sentiment' => Question::choice('What is the overall sentiment of this review?', [
            'positive' => 'praises the product',
            'neutral'  => 'describes the product without clear praise or criticism',
            'negative' => 'criticises the product',
        ]),
    ]);

    $spam      = $result->yesNo('spam');
    $abuse     = $result->yesNo('abuse');
    $sentiment = $result->choice('sentiment');

    $needsReview = $spam->yes()
        || $abuse->yes()
        || min($spam->answerConfidence, $abuse->answerConfidence, $sentiment->answerConfidence) < 0.7;

    return [
        'queue'     => $needsReview ? 'manual-review' : 'publish',
        'sentiment' => $sentiment->choice,
    ];
}

```

`yes()` uses a default probability threshold of `0.5`. The separate `answerConfidence` value measures how certain the model is about each answer. Routing low-confidence results to a human reviewer is a sensible safety net — the right threshold for your application should be calibrated against decisions your moderators have already made.

### Other Question Types

`Question::score()` handles ordered scales such as support-ticket urgency levels. The answer includes the most likely level and a numeric score. The `decide()` method maps questions defined with PHP attributes directly onto enum, integer, and boolean constructor properties, keeping classification logic close to your domain models.

Installation and Setup
----------------------

LayaPHP requires **PHP 8.4 or newer** and a PSR-18 HTTP client:

```bash
composer require marcreichel/laya-php guzzlehttp/guzzle

```

The package repository ships a Docker Compose file for `laya-serve`. Start it with:

```bash
docker compose up -d --wait

```

The first classification request downloads approximately 1 GB of model weights into a Docker volume. Once the server is running at `http://localhost:8000`:

1. Add `LAYA_URL=http://localhost:8000` to your `.env` file.
2. Laravel 13 or newer auto-discovers the service provider.
3. Inject `Laya` into any job or service class.
4. Run `php artisan laya:health` to verify the server is reachable.

Outside Laravel, instantiate the client directly: `new Laya('http://localhost:8000')`.

### Testing Without a Running Model

`Laya::fake()` lets you write unit tests for routing and business logic without starting the model server. Testing whether Laya itself classifies text correctly still requires a live `laya-serve` instance.

Key Takeaways
-------------

- Laya is a self-hosted decision model — no external API fees, full data control.
- LayaPHP returns typed PHP answers (yes/no, choice, score) with per-answer confidence values.
- A single `predict()` call can ask multiple questions simultaneously.
- PHP 8.4+ is required; Laravel 13+ auto-discovers the package.
- Docker Compose setup is included; the first run downloads ~1 GB of model weights.
- `Laya::fake()` enables unit testing without a live model server.

---

*Source: [LayaPHP: Self-Hosted Text Classification for PHP and Laravel — Laravel News](https://laravel-news.com/laya-php-self-hosted-classification)*

- [Laravel](https://www.msaied.com/articles?search=Laravel)
- [PHP](https://www.msaied.com/articles?search=PHP)
- [Text Classification](https://www.msaied.com/articles?search=Text%20Classification)
- [Machine Learning](https://www.msaied.com/articles?search=Machine%20Learning)
- [Open Source](https://www.msaied.com/articles?search=Open%20Source)
- [Composer Package](https://www.msaied.com/articles?search=Composer%20Package)

 Frequently asked questions 
---------------------------

  Does LayaPHP require an external API subscription to classify text?No. Laya runs entirely on your own infrastructure via a `laya-serve` process. You pay only for the compute you use — there is no per-call API charge.

   What PHP version does LayaPHP require?LayaPHP requires PHP 8.4 or newer, along with a PSR-18 HTTP client such as Guzzle.

   Can I write unit tests for LayaPHP-based logic without running the model server?Yes. The built-in `Laya::fake()` helper lets you mock responses and test your routing and business logic without a live `laya-serve` instance. Testing the model's actual classification accuracy still requires a running server.

   ![Mohamed Said](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp)About the author
----------------

[Mohamed Said](https://www.msaied.com#person)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

[About](https://www.msaied.com#about) [GitHub ↗](https://github.com/EG-Mohamed) [LinkedIn ↗](https://www.linkedin.com/in/msaiedm/) [WhatsApp ↗](https://wa.me/201094619204) [Email Address ↗](mailto:hello@msaied.com) [My CV ↗](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)  

   [Previous articleMySQL Full-Text Search in Laravel: Indexes, Relevance Scoring, and Boolean Mode](https://www.msaied.com/articles/mysql-full-text-search-in-laravel-indexes-relevance-scoring-and-boolean-mode) [Next articleThe Pipeline Pattern in Laravel: Custom Pipelines Beyond Middleware](https://www.msaied.com/articles/the-pipeline-pattern-in-laravel-custom-pipelines-beyond-middleware-1)  

   On this page
-------------

1. [What Is LayaPHP?](#what-is-layaphp)
2. [Classifying a Product Review](#classifying-a-product-review)
3. [Other Question Types](#other-question-types)
4. [Installation and Setup](#installation-and-setup)
5. [Testing Without a Running Model](#testing-without-a-running-model)
6. [Key Takeaways](#key-takeaways)

 ###  Have a technical challenge?

 Tell me what you’re building. I reply within two working days.

[Start a conversation](https://www.msaied.com#contact) 

   Related articles
-----------------

 [ ![](https://cdn.msaied.com/743/8998fac3a41451ab3fe1588194e17a43.png) Filament · 3 min read### Securing Filament Plugins with Plumb: Automated Security Scoring for PHP Packages

5 Oct 2026 ](https://www.msaied.com/articles/securing-filament-plugins-with-plumb-automated-security-scoring-for-php-packages) [ ![](https://cdn.msaied.com/742/2d02018669cdeedccb5de2efb898f0ee.png) Filament · 3 min read### Filament v3.3.56 Released: File Hash Names and Livewire Upload Fix

5 Oct 2026 ](https://www.msaied.com/articles/filament-v3356-released-file-hash-names-and-livewire-upload-fix) [ ![](https://cdn.msaied.com/741/5b55c123ad08e4d34e1f4b99ad6a428b.png)  · 3 min read### Filament v4 Schema-Based Forms, Infolists, and the Unified Schema API

5 Oct 2026 ](https://www.msaied.com/articles/filament-v4-schema-based-forms-infolists-and-the-unified-schema-api-5) 

  Have a technical challenge?
----------------------------

Tell me what you’re building. I reply within two working days.

 [Discuss your project ↗](https://www.msaied.com#contact) 

  © 2026 Mohamed Said · Built with Laravel, meant to last.Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

 - [Home](https://www.msaied.com)
- [Articles](https://www.msaied.com/articles)
- [Certificates](https://www.msaied.com/certificates)
- [GitHub](https://github.com/EG-Mohamed)
- [LinkedIn](https://www.linkedin.com/in/msaiedm/)
- [WhatsApp](https://wa.me/201094619204)
- [Email Address](mailto:hello@msaied.com)
- [My CV](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
- [Sitemap](https://www.msaied.com/sitemap.xml)
