mphpmaster/laravel-guesser-helpers
Laravel Guesser Helpers is a lightweight utility package that adds convenient guessing helpers to Laravel applications. It wraps common detection logic behind simple, expressive functions, making it easy to infer values without extra boilerplate. Drop it in and use the helpers anywhere in your app.
mPhpMaster/laravel-guesser-helpers is a small utility package that centralises the "guess the convention" logic Laravel apps tend to scatter around: resolving a model class from a table/route string, inferring table names, relation names, foreign keys, and similar naming conventions. Version 1.0.0 is the first tagged release, so treat the API as young and pin it explicitly.
Minimal steps:
composer require mphpmaster/laravel-guesser-helpers:1.0.0
php artisan tinker
>>> function_exists('guess_model') // or whatever the README documents
composer.json, register the provider manually in bootstrap/providers.php (Laravel 11+) or config/app.php.src/ directory and README — 1.0.0 shipped with no meaningful release notes beyond a changelog link, so the source is the authoritative reference for exact function names and signatures.php artisan vendor:publish --tag=guesser-helpers) and whether it exposes a namespace/prefix mapping you should set for your app.First use case: a generic admin/resource controller where you currently hardcode model class strings per resource. Replace the hardcoded map with a guesser call keyed off the route segment, then override only the exceptions.
Route::resource('blog-posts', PostController::class);
class PostController extends Controller
{
public function index(string $resource)
{
$model = guess_model($resource); // 'blog-posts' -> App\Models\BlogPost
return $model::paginate();
}
}
Convention-first, config-second resolution. The productive pattern is to let the guesser handle the 80% that follow Laravel conventions and keep a thin override map for the rest. Wrap the package rather than calling it inline everywhere:
final class ResourceResolver
{
public function model(string $resource): string
{
return config("resources.overrides.$resource")
?? guess_model($resource);
}
}
This keeps a single seam for testing and lets you swap the package later without touching controllers.
Generic CRUD and scaffolding. Guessers shine in code that is deliberately model-agnostic: base resource controllers, generic DataTables/ApiResource endpoints, artisan commands that generate files from a table name, and stub-based scaffolding. In each case the guesser removes the need for a hand-maintained registry that drifts as the app grows.
Validation rules. Form requests frequently need the table name for unique/exists rules where the model is dynamic. Resolving it through the guesser keeps the rule consistent with the model's actual table, including when a model overrides $table.
$table = guess_table(Model::class);
$rules = ['email' => ["unique:$table,email"]];
Route model binding fallbacks. For catch-all or CMS-style routes where the segment is user-defined, use the guesser to attempt a model resolution and then fall back to a 404 or an explicit map. Always validate the guessed class with class_exists() and is_subclass_of($class, Model::class) before querying — never pass a guessed string straight into app() or new.
Testing. Add one test that iterates your app/Models directory and asserts the guesser round-trips each model back to its table and expected route key. This catches the moment a convention drifts and turns a silent runtime failure into a failing test.
it('guesses correctly for every model', function () {
foreach (File::files(app_path('Models')) as $file) {
$class = 'App\\Models\\' . $file->getFilenameWithoutExtension();
expect(guess_table($class))->toBe((new $class)->getTable());
}
});
Integration tips. Call guessers at the edge (request/command entry points), not deep in domain code — guessed values are inherently unreliable and should be resolved once, validated, then passed as concrete values. If you use the package inside a loop over many resources, memoise results in a static array or a small cache; reflection-based guessing is not free.
composer.json and diff src/ before any upgrade. Expect breaking changes in helper names or return types in the 1.x line until the API stabilises.$table, a non-standard namespace, a $primaryKey override, or a morph map will defeat naive conventions. Anything polymorphic should bypass the guesser entirely and use explicit config.Person, Category, BlogPost → blog_posts) are where guessers most often diverge from your schema. Cover these in the round-trip test above.Cannot redeclare after install, run composer dump-autoload -o and check for duplicates before filing a bug.tinker, call the guesser directly against the string and the class to see which direction is failing, then inspect the model's getTable(), getForeignKey(), and namespace. Nine times out of ten the input format (kebab-case vs snake_case vs StudlyCase) is the culprit, not the package.App\Domain\... layout; otherwise subclass or wrap the helpers behind your own resolver as shown above. Avoid editing vendor files — 1.0.0 has no BC guarantees to protect you.How can I help you explore Laravel packages today?