Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x]Update with DB::raw triggers Object of class Illuminate\Database\Query\Expression could not be converted to float #42645

Closed
LiXunGuan opened this issue Jun 3, 2022 · 2 comments

Comments

@LiXunGuan
Copy link

  • Laravel Version: 8.83.7
  • PHP Version: 7.4.3
  • Database Driver & Version: mysql8.x

Description:

When I define the $casts converter for the Inventory model, using DB::raw gives an error.

Steps To Reproduce:

1、Inventory Model

    protected $casts = [
        'amount' => 'float',
        'number' => 'float'
    ];

2、Controller

        $batchs->inventory->updateOrCreate(
            [
                'warehouse_id' => $batchs->warehouse_id,
                'goods_id'     => $batchs->goods_id,
            ],
            [
                'number' => DB::raw('number + ' . $batchs->number),
                'amount' => DB::raw('amount + ' . $batchs->amount)
            ]
        );

error message stack

[2022-06-03 16:14:34] production.ERROR: Object of class Illuminate\Database\Query\Expression could not be converted to float {"userId":3,"exception":"[object] (ErrorException(code: 0): Object of class Illuminate\Database\Query\Expression could not be converted to float at D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php:1248)
[stacktrace]
#0 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(1248): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Object of class...', 'D:\\wwwroot\\his\\...', 1248, Array)
#1 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(720): Illuminate\Database\Eloquent\Model->fromFloat(Object(Illuminate\Database\Query\Expression))
#2 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(1966): Illuminate\Database\Eloquent\Model->castAttribute('number', Object(Illuminate\Database\Query\Expression))
#3 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(1918): Illuminate\Database\Eloquent\Model->originalIsEquivalent('number')
#4 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(1852): Illuminate\Database\Eloquent\Model->getDirty()
#5 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(986): Illuminate\Database\Eloquent\Model->isDirty()
#6 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(509): Illuminate\Database\Eloquent\Model->save()
#7 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Support\helpers.php(263): Illuminate\Database\Eloquent\Builder->Illuminate\Database\Eloquent\{closure}(Object(App\Models\Inventory))
#8 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(510): tap(Object(App\Models\Inventory), Object(Closure))
#9 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php(23): Illuminate\Database\Eloquent\Builder->updateOrCreate(Array, Array)
#10 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(2132): Illuminate\Database\Eloquent\Model->forwardCallTo(Object(Illuminate\Database\Eloquent\Builder), 'updateOrCreate', Array)
#11 D:\wwwroot\his\app\Models\InventoryBatchs.php(111): Illuminate\Database\Eloquent\Model->__call('updateOrCreate', Array)
#12 D:\wwwroot\his\app\Models\InventoryBatchs.php(27): App\Models\InventoryBatchs::updateOrCreateInventory(Object(App\Models\InventoryBatchs))
#13 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php(404): App\Models\InventoryBatchs::App\Models\{closure}(Object(App\Models\InventoryBatchs))
#14 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php(249): Illuminate\Events\Dispatcher->Illuminate\Events\{closure}('eloquent.create...', Array)
#15 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasEvents.php(189): Illuminate\Events\Dispatcher->dispatch('eloquent.create...', Array)
#16 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1174): Illuminate\Database\Eloquent\Model->fireModelEvent('created', false)
#17 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(994): Illuminate\Database\Eloquent\Model->performInsert(Object(Illuminate\Database\Eloquent\Builder))
#18 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Relations\HasOneOrMany.php(296): Illuminate\Database\Eloquent\Model->save()
#19 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Support\helpers.php(263): Illuminate\Database\Eloquent\Relations\HasOneOrMany->Illuminate\Database\Eloquent\Relations\{closure}(Object(App\Models\InventoryBatchs))
#20 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Relations\HasOneOrMany.php(297): tap(Object(App\Models\InventoryBatchs), Object(Closure))
#21 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Relations\HasOneOrMany.php(311): Illuminate\Database\Eloquent\Relations\HasOneOrMany->create(Array)
#22 D:\wwwroot\his\app\Http\Controllers\PurchaseController.php(192): Illuminate\Database\Eloquent\Relations\HasOneOrMany->createMany(Array)
#23 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Routing\Controller.php(54): App\Http\Controllers\PurchaseController->check(Object(App\Http\Requests\Purchase\CheckRequest))
#24 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('check', Array)
#25 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Routing\Route.php(262): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\PurchaseController), 'check')
#26 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Routing\Route.php(205): Illuminate\Routing\Route->runController()
#27 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Routing\Router.php(721): Illuminate\Routing\Route->run()
#28 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(128): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#29 D:\wwwroot\his\app\Http\Middleware\Permission.php(19): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#30 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): App\Http\Middleware\Permission->handle(Object(Illuminate\Http\Request), Object(Closure), 'purchase.manage')
#31 D:\wwwroot\his\app\Http\Middleware\Login.php(51): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#32 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): App\Http\Middleware\Login->handle(Object(Illuminate\Http\Request), Object(Closure))
#33 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php(50): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#34 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#35 D:\wwwroot\his\vendor\stancl\tenancy\src\Middleware\IdentificationMiddleware.php(36): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#36 D:\wwwroot\his\vendor\stancl\tenancy\src\Middleware\InitializeTenancyByDomain.php(38): Stancl\Tenancy\Middleware\IdentificationMiddleware->initializeTenancy(Object(Illuminate\Http\Request), Object(Closure), '127.0.0.1')
#37 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Stancl\Tenancy\Middleware\InitializeTenancyByDomain->handle(Object(Illuminate\Http\Request), Object(Closure))
#38 D:\wwwroot\his\vendor\stancl\tenancy\src\Middleware\PreventAccessFromCentralDomains.php(29): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#39 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains->handle(Object(Illuminate\Http\Request), Object(Closure))
#40 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#41 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Routing\Router.php(723): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#42 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Routing\Router.php(698): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#43 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Routing\Router.php(662): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))
#44 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Routing\Router.php(651): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#45 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(167): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#46 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(128): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#47 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#48 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull.php(31): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#49 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull->handle(Object(Illuminate\Http\Request), Object(Closure))
#50 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#51 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TrimStrings.php(40): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#52 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TrimStrings->handle(Object(Illuminate\Http\Request), Object(Closure))
#53 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#54 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#55 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance.php(86): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#56 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance->handle(Object(Illuminate\Http\Request), Object(Closure))
#57 D:\wwwroot\his\vendor\fruitcake\laravel-cors\src\HandleCors.php(38): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#58 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Fruitcake\Cors\HandleCors->handle(Object(Illuminate\Http\Request), Object(Closure))
#59 D:\wwwroot\his\vendor\fideloper\proxy\src\TrustProxies.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#60 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Fideloper\Proxy\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))
#61 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#62 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(142): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#63 D:\wwwroot\his\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(111): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#64 D:\wwwroot\his\public\index.php(52): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#65 D:\wwwroot\his\server.php(21): require_once('D:\\wwwroot\\his\\...')
#66 {main}
"}

@driesvints
Copy link
Member

It's not supported to use DB::raw here. Please see the docs on how to use DB::raw https://laravel.com/docs/9.x/queries#raw-expressions

@LiXunGuan
Copy link
Author

LiXunGuan commented Jun 4, 2022

Other ways to achieve, documented for reference by others.

        $inventory = $batchs->inventory()->firstOrNew([
            'warehouse_id' => $batchs->warehouse_id,
            'goods_id'     => $batchs->goods_id,
        ]);

        // update
        $inventory->number = bcadd($inventory->number, $batchs->number, 4);
        $inventory->amount = bcadd($inventory->amount, $batchs->amount, 4);

        $inventory->save();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants