sambenge/codeigniter4-sole
Adds Laravel's sole() model method to CodeIgniter 4.
Maintainers
1.0.3
2025-10-08 23:29 UTC
Requires
- php: >=7.4
Requires (Dev)
None
Suggests
- codeigniter4/framework: ^4.0 - This package is designed for CodeIgniter 4 projects
Provides
None
Conflicts
None
Replaces
None
MIT 0b47db0c474be001c14002adef1c35f1657d9b7c
- Sam Benge <sam.woop@bengey.co.uk>
This package is not auto-updated.
Last update: 2026-06-19 01:02:01 UTC
README
A lightweight package that adds Laravel-style sole() behaviour to CodeIgniter 4 models.
Ensures that a query returns exactly one record, otherwise throws an exception.
🚀 Installation
From Packagist (recommended)
composer require sambenge/codeigniter4-sole
⚙️ Usage
In your model:
use CodeIgniter\Model; use Bengey\Sole\SoleTrait; class UserModel extends Model { use SoleTrait; protected $table = 'users'; }
Then, in your service or controller:
$userModel = new UserModel(); try { $user = $userModel->where('email', 'john@smith.com')->sole(); // Do something with $user } catch (\Bengey\Sole\Exceptions\RecordNotFoundException $e) { // No records found } catch (\Bengey\Sole\Exceptions\MultipleRecordsFoundException $e) { // More than one record found }
🧠 Why use sole()?
The sole() method is useful when you expect a query to return exactly one record. It helps to enforce this expectation by throwing exceptions if the result set is empty or contains multiple records. This can help to prevent bugs and ensure that your application behaves as expected.
