nicolaslopezj / searchable

A php trait to search laravel models
MIT License
2.01k stars 291 forks source link

Conflict with eloquent softDeletes #101

Open zenn1989 opened 8 years ago

zenn1989 commented 8 years ago

If in active record used SearchableTrait with softDeletes (look at Illuminate\Database\Eloquent\SoftDeletes) search will crash with exception:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'contents.deleted_at' in 'where clause' (SQL: select * from (select `contents`.*, max((case when LOWER(`title`) LIKE test then 75 else 0 end) + (case when LOWER(`title`) LIKE test% then 25 else 0 end) + (case when LOWER(`title`) LIKE %test% then 5 else 0 end) + (case when LOWER(`text`) LIKE test then 15 else 0 end) + (case when LOWER(`text`) LIKE test% then 5 else 0 end) + (case when LOWER(`text`) LIKE %test% then 1 else 0 end)) as relevance from `contents` where `contents`.`deleted_at` is null group by `contents`.`id` having relevance > 1.50 order by `relevance` desc) as `contents` where `contents`.`deleted_at` is null)

take a look on latest where condition - seems to be wrong in this query. Query without latest stage:

where `contents`.`deleted_at` is null group by `contents`.`id`

is ended without errors. From controller it looks like:

$search = Content::search('test')->get();

active records writed as:

use Illuminate\Database\Eloquent\SoftDeletes;
use Nicolaslopezj\Searchable\SearchableTrait;

class Content extends ActiveModel
{
    use SearchableTrait, SoftDeletes;

    protected $searchable = [
        'columns' => [
            'title' => 5,
            'text' => 1
        ]
    ];
}

Also, not the best solution is skip 'softDeletes' checking:

Content::search('test')->withTrashed()->get();

Hope you'll fix this bug, if it available in this trait (if it's not softDeletes misstake).

Fanxd commented 5 years ago

database.php mysql strict => false