Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Floats Laravel Package

fisharebest/laravel-floats

A Laravel package for handling floating-point numbers safely. It offers an Eloquent cast and helpers to store, retrieve, and compare floats without precision or locale issues, keeping decimal attributes consistent across your database and application.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install the package

    composer require fisharebest/laravel-floats
    

    There is no config file to publish and no .env keys — the package is a single-purpose schema extension.

  2. Check auto-discovery. Laravel 5.5+ will pick up the package's service provider from composer.json. On older versions, add Fisharebest\LaravelFloats\...\ServiceProvider::class to the providers array in config/app.php.

  3. Look first at the service provider inside vendor/fisharebest/laravel-floats/src/. That file tells you exactly how the float method is grafted onto the Blueprint (macro vs. custom grammar). The whole package is ~1–3 files, so read it before assuming behaviour.

  4. First use case — add a float column in a normal migration:

    Schema::create('locations', function (Blueprint $table) {
        $table->increments('id');
        $table->float('latitude');
        $table->float('longitude');
    });
    

    Run php artisan migrate, then confirm with SHOW CREATE TABLE locations\G — you should see FLOAT (not DOUBLE or DECIMAL).

  5. Smoke-test rollback with php artisan migrate:rollback to make sure the column drop path is sane before relying on it.

Implementation Patterns

  • Create and alter migrations are symmetric. Use $table->float('col') in Schema::create and Schema::table callbacks identically; the macro is available on any Blueprint instance.

  • Optional precision/scale. Mirror MySQL's legacy FLOAT(M,D) syntax: $table->float('ratio', 8, 3). Omit the arguments on MySQL 8.0.17+ where FLOAT(M,D) is deprecated.

  • Chain modifiers as usual: $table->float('weight')->nullable()->default(0.0). Verify each modifier you use actually lands — see Gotchas.

  • Cast in your model. MySQL's PDO driver returns FLOAT as a string, so add:

    protected $casts = [
        'latitude'  => 'float',
        'longitude' => 'float',
    ];
    
  • Converting existing columns. The package usually doesn't implement ->change(). Safe pattern: add the new float column, backfill with a query, drop the old column, then re-add any indexes. Never rely on a single ->change() call.

  • Portable wrapper macro (if you want SQLite tests to keep working):

    Blueprint::macro('singleFloat', function ($column, $total = null, $places = null) {
        return DB::getDriverName() === 'mysql'
            ? $this->float($column, $total, $places)
            : $this->double($column, $total, $places);
    });
    
  • Large tables. MySQL may rewrite the whole table for the resulting ALTER. In production, apply with pt-online-schema-change or gh-ost and mark the migration as already run.

  • Testing. Point your test suite at MySQL (Docker/CI service), not SQLite. Feature tests that touch the schema will diverge otherwise.

  • Extension point. The package is small enough that if you need variants (custom grammar for Postgres, a float4/float8 switch, etc.), copy its service provider into your own app/Providers and register the macro yourself. That gives you full control and removes a low-maintenance dependency.

Gotchas and Tips

  • Check Laravel's native support first. Recent Laravel versions ship $table->float() in Illuminate\Database\Schema\Blueprint. If you're on one of them, this package is redundant — search that file for function float before adding a dependency.

  • "Method float does not exist" means the macro wasn't registered. Confirm the package's composer.json declares extra.laravel.providers, run php artisan package:discover, and fall back to manual registration in config/app.php if auto-discovery doesn't fire.

  • MySQL-only. The grammar hook targets MySQL; SQLite and PostgreSQL will either error or produce unexpected types. Keep a MySQL instance in your test stack.

  • FLOAT(M,D) is deprecated since MySQL 8.0.17 and removed in newer releases. Pass no arguments unless you specifically need legacy compatibility.

  • Precision trap. FLOAT is single-precision (~7 significant digits). Perfect for coordinates, sensor readings, and ratios; never for currency — use decimal() there.

  • String values from PDO. Without a model cast, FLOAT columns come back as strings. Debug with dd(gettype($model->latitude)) if JSON responses look like "12.5" instead of 12.5.

  • Schema::getColumnType() varies by Laravel version (some return 'double' for anything float-like). Use Schema::getColumns('table') on Laravel 10+ or SHOW COLUMNS for ground truth.

  • Modifiers aren't guaranteed. ->unsigned(), ->comment(), and custom defaults may be silently dropped depending on how the grammar hook is implemented. Inspect the generated DDL (php artisan migrate --pretend) before shipping.

  • ->change() is unsupported. Don't assume schema-diffing tools that rely on change() will work; use explicit add/drop or raw DB::statement('ALTER TABLE … MODIFY … FLOAT').

  • Debugging checklist: in Tinker, run Illuminate\Database\Schema\Blueprint::hasMacro('float'), then Schema::getColumns('your_table') after migrating. If both fail, the provider isn't booted.

  • Long-term maintenance. With 11 stars and a narrow scope, expect minimal updates. Forking or vendoring the macro into your app is a reasonable hedge if you can't drop the package.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
frankyso/ipaymu
3mdikram/worldmoneywords
snowsoft/config
itmosoft/laravel-subscriptions
larva/laravel-ai-qwen
adachsoft/api-version-bundle
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core