teamtnt / laravel-scout-tntsearch-driver

Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch
MIT License
1.1k stars 144 forks source link

Laravel model with relationships replicate fails on save/push mb_strtolower() expects parameter 1 to be string, array given #263

Closed twigg closed 3 months ago

twigg commented 4 years ago

When trying to replicate a model with relationships TNTSearch fails with mb_strtolower() expects parameter 1 to be string, array is given error.

This only happens when Laravel replicates a model with relationships and then tries to save it. This behavior doesn't happen for models without relationships. Same outcome for save() and push()

Example:

This works fine as expected.

$inventory = Inventory::where(['id' => $order->inventory_id])->first();
$new = $inventory->replicate();
$new->push();

TNTSearch will fail with the following:

$inventory = Inventory::with(['type', 'form'])->where(['id' => $order->inventory_id])->first();
$new = $inventory->replicate();
$new->push();
marky291 commented 3 years ago

image

websitevirtuoso commented 1 year ago

I have the same problem. whe use next example code

return DB::transaction(function () use ($args) {
            $user = User::where('id', $args['id'])->firstOrFail();

            $user->fill($args);
            $user->save();

            $roles = Role::whereIn('id', $args['role_id'])->pluck('id')->toArray();
            $user->syncRoles($roles);

            return $user;
        });

If I disable tntsearch for user class everything works. I can create reproduction if you really need more info. I had to disable tnt for whole project.

websitevirtuoso commented 1 year ago

I could solve it. By define SearchableArray and exclude all arrays from this list It can be only primitives

public function toSearchableArray(): array
    {
        return [
            'id'          => $this->id,
            'first_name'  => $this->first_name,
            'last_name'   => $this->last_name,
            'status'      => $this->status,
            'address'     => $this->address,
            'postal_code' => $this->postal_code,
            'email'       => $this->email,
        ];
    }