Laravel自定义Yajra服务端Datatable报错:A non well formed numeric value encountered
Hey there, let's tackle this error you're facing with Yajra Datatables in Laravel. That "non well formed numeric value" warning almost always comes down to a type mismatch—somewhere in your code, a non-numeric value (like a string, null, or formatted text) is being treated as a number for arithmetic, sorting, or pagination. Let's walk through the most likely fixes step by step:
1. Check Datatable Request Parameter Casting
Yajra sends Ajax parameters like start, length, and column indexes that power pagination and sorting. If these aren't explicitly cast to integers, PHP can throw this error when trying to calculate offsets or target columns.
In your custom Datatable class (or wherever you handle the incoming request), cast these values to integers upfront:
// Cast pagination parameters to integers $start = (int) $request->get('start'); $length = (int) $request->get('length'); // Cast column index for sorting to integer $columnIndex = (int) $request->get('order')[0]['column'];
If you're using Yajra's built-in query() method, double-check that you aren't overriding pagination logic with uncast string values.
2. Audit Your Data Transformation Logic
The error often pops up in the function where you convert Eloquent models to Datatable rows. Look for any arithmetic operations on values that might be stored as strings (e.g., currency with $ symbols, formatted numbers) or nullable fields that come through as null.
Example of a problematic line:
'total_amount' => $model->price + $model->tax,
If price or tax are strings (like "$100.00") or null, PHP will throw the non-numeric error. Fix this by ensuring values are numeric first:
// Clean and cast string values to floats 'total_amount' => (float) str_replace(['$', ','], '', $model->price) + (float) str_replace(['$', ','], '', $model->tax),
Or better yet, use Laravel's model casting to auto-convert fields to numeric types:
// In your Eloquent model protected $casts = [ 'price' => 'float', 'tax' => 'float', ];
3. Verify Sorting Column Handling
If you're manually handling sorting (instead of letting Yajra auto-detect columns), ensure the column index you're using is a numeric value. A common mistake is passing a string index where an integer is expected.
For example, make sure $columnIndex is cast to int before using it to fetch the sort column:
$columnIndex = (int) $request->get('order')[0]['column']; $sortColumn = $request->get('columns')[$columnIndex]['data']; $query->orderBy($sortColumn, $request->get('order')[0]['dir']);
4. Check Blade Table Attributes
In your Blade template, if you're using custom data-* attributes for Datatables (like data-order), ensure those values are numeric if Datatables expects them. Avoid passing string values to attributes that control sorting or ordering:
<!-- Bad: Passing a string to data-order --> <th data-order="{{ $someNonNumericString }}">Total</th> <!-- Good: Using a numeric value or letting Datatables handle it --> <th data-order="{{ $model->id }}">Total</th>
5. Pinpoint the Exact Error Line
Enable Laravel's debug mode (set APP_DEBUG=true in your .env file) and check the stack trace. It will show you the exact line of code causing the error—this is the fastest way to narrow down the issue. For example, if the error points to a line where you're calculating $start + $length, you know immediately that $start or $length isn't a valid number.
Once you fix the type mismatch in the problematic line, the error should disappear. The key is to ensure any value used in a numeric context (arithmetic, pagination, sorting) is explicitly cast to a number or validated as numeric first.
内容的提问来源于stack exchange,提问作者Fabio Stritt

