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

Ahmed Alaa's avatar

How to search on appended field ?

I have a text field in a MySQL table that has some characters that I want to ignore while I'm searching in this field but appear in the application normally. so, I thought to return two copies of this field the original one for displaying and another one for searching I've used the append method, but unfortunately, I couldn't search in it using where (like)

// Append method
 public function getTestAttribute()
    {
        return preg_replace("~[\x{064B}-\x{065B}]~u","",$this->f_name);
    }
// Search
$users = User::with(['phones', 'roles', 'cities', 'countries'])
            ->where(
                fn ($q) => $q
                    ->whereDoesntHave('roles')
                    ->orwhereHas("roles", function ($q) {
                        $q->where("id", 1);
                    })
            )
            ->where(
                fn ($q) => $q
                    ->where('f_name->en', 'like', '%' . $this->search . '%')
                    ->orWhere('f_name->ar', 'like', '%' . $this->search . '%')
                    ->orWhere('test', 'like', '%' . $this->search . '%') // Here
                    ->orWhere('l_name->en', 'like', '%' . $this->search . '%')
                    ->orWhere('l_name->ar', 'like', '%' . $this->search . '%')
                    ->orWhere('email', 'like', '%' . $this->search . '%')
                    ->orWhereHas('phones', function ($query) {
                        $query->where('phone', 'like', '%' . $this->search . '%');
                    })
            )
0 likes
2 replies
Sinnbeck's avatar

No you cannot search a "fake" column in php inside your database. It needs to be actually present in the database to be searchable

1 like
Ahmed Alaa's avatar

@Sinnbeck Thank you for your answer. But, Can I manipulate the text while using where (like) ?

Please or to participate in this conversation.