Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Bogey's avatar
Level 1

Laravel Scout: Database driver issue

I got Laravel scout and I'm using the database driver and trying to get it set-up to work but I can't get it to work. It's simply retrieving everything.

I've ran the

php artisan scout:import "App\Models\Blog"

My Blog model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Blog extends Model
{
    use HasFactory, Searchable;
    
    // irrelavent stuff snipped
    
    public function tags()
    {
        return $this->belongsToMany(Tags::class, 'tags_posts');
    }

    public function toSearchableArray()
    {
        if ($this->published === true) {
            return [
                'id' => $this->id,
                'post_title' => $this->post_title,
                'post_context' => $this->post_context,
                'post_body' => $this->post_body,
                'tags'  => $this->tags->pluck('tag_name')->toArray()
            ];
        }

        return [];
    }
}

SearchController

    public function search(Request $request)
    {
        DB::enableQueryLog();
        $post = Blog::search($request['search'])->get()->toArray();
        dd(DB::getQueryLog());
        echo '<pre>';
        print_r($post);
        echo '</pre>';
    }

That generates

  0 => array:3 [▼
    "query" => "select * from `blogs` order by `id` desc"
    "bindings" => []
    "time" => 0.54
  ]
1 like
1 reply
luchaos's avatar

Just in case anyone is looking for an answer; I spent some time debugging because I ran into the same issue:

The reason for this is that in the example above toSearchableArray() always returns an empty array. $this->published cannot be true because no model is loaded at the time Scout is calling this method. Because it uses this method to gather the searchable columns it must not be dynamic.

An empty array returned by toSearchableArray() causes the DatabaseEngine not finding any column keys to be included in the query, resulting in returning everything.

I ran into it because I used it like in the docs (https://laravel.com/docs/12.x/scout#configuring-searchable-data)

public function toSearchableArray(): array
{
    return $this->toArray();
}

Because there is no loaded model, there are no attribute keys to be found via $this->toArray() thus an empty array is returned.

I think the docs are actually misleading by mentioning $this->toArray() at all because it does not work like this with the database engine. I had to change it to an explicit definition, like in all the other examples in the docs, so it can find the keys at all times; it must not be dynamic.

public function toSearchableArray(): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
    ];
}

To make sure only published posts are added to the index the shouldBeSearchable() method should be used as stated in the docs: https://laravel.com/docs/12.x/scout#conditionally-searchable-model-instances

In short: make sure toSearchableArray() returns an array with column keys explicitly, not dynamically.

Please or to participate in this conversation.