laravel-auditing icon indicating copy to clipboard operation
laravel-auditing copied to clipboard

Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given

Open LBreda opened this issue 5 years ago • 3 comments

Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 5.8.36
Package version 9.3.2
PHP version 7.2.26

Actual Behaviour

Any operation that uses the Audit events fails with an exception:

Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given

Expected Behaviour

I'm expecting the library to just work.

Steps to Reproduce

I simply have a model that implements \OwenIt\Auditing\Contracts\Auditable and uses OwenIt\Auditing\Auditable, and I try to save records on it via Eloquent. Nothing fancy.

Possible Solutions

Trying to debug, I found out that the compileInsert function in Illuminate\Database\Query\Grammars\Grammar.php tries o determine if the $values input is an array of records (arrays) or if it is a single record (array). In the second case, it wraps it in a array,

My $values variable is something like this, when I try to insert a record:

Array
(
    [old_values] => Array
        (
        )

    [new_values] => Array
        (
            [name] => 3332
            [birth_date] => 2020-01-30
            [sw_version] => 32
            [part_no] => 13
            [serial_no] => 654765te5
            [category_id] => 5
            [type_id] => 2
            [attributes_json] => {"tot_km":null,"old_km":[],"old_sw":[{"sw":"32","date":"01-2020"}]}
            [id] => 140
        )

    [event] => created
    [auditable_id] => 140
    [auditable_type] => App\Http\Models\SystemElements
    [user_id] => 1
    [user_type] => App\Http\Models\User
    [url] => http://gimmi-skg.local/system_elements?train_id=1
    [ip_address] => 192.168.10.1
    [user_agent] => Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0
    [tags] => 
    [updated_at] => 2020-01-30 12:58:09
    [created_at] => 2020-01-30 12:58:09
)

Being the first value an array, the function believes that $values is not a single record, and does not wrap it. When the same function later tries to parametrize each elements of $values it fails on the first non-array element (`'event' => 'created').

Probably old_values and new values should be json strings at this point.

LBreda avatar Jan 30 '20 11:01 LBreda

Worked it around by adding:

protected $casts = [
        'old_values' => 'json',
        'new_values' => 'json',
    ];

to the Audits model. Didn't understand why it needs it, tho.

LBreda avatar Jan 30 '20 12:01 LBreda

butwith arabic chars return as \u0627\u0644\u0645\u0642\u064a\u064a\u064a

thaifani avatar Nov 22 '20 15:11 thaifani

@lBreda u saved my day!

fayzasalman avatar Feb 09 '21 09:02 fayzasalman

https://github.com/owen-it/laravel-auditing/issues/570#issuecomment-580221994

parallels999 avatar Mar 08 '23 15:03 parallels999