High line Query Search Result Text
Hello,
When i input any keyword and search , I just want to high line or blold or something, for those match query result text.
protected function keyword()
{
$query = request()->input('keyword');
return $this->builder->where('title', 'LIKE', "%$query%");
}
Thanks
$keyword = request()->input('keyword');
$results = NameOfModel::where('title', 'LIKE', "%$keyword%")->get()->transform(function ($item) use ($keyword) {
$item->mark_title = preg_replace('/('.$keyword.')/i', "<mark>$1</mark>", $item->title);
return $item;
});
And then you need to style <mark> tag.
For example
mark {
background: #fbe9e7;
padding: 0 5px;
color: #000;
font-weight: bold;
display: inline-block;
}
And of course instead of title you will use mark_title
@foreach ($results as $result)
{!! $result->mark_title !!}
@endforeach
Hi Michal,
Thanks for your reply
Because of i have another query filter section like date filter, view_count filter, May be i made wrong something below , No high line anymore
return $this->builder->where('title', 'LIKE', "%$keyword%")->get()->transform(function ($item) use ($keyword) {
$item->title = preg_replace('/('.$keyword.')/i', "", $item->title);
return $item;
});
need return $item or not ?
Thanks again
Of course you need to return $item in that example.
I don't what is your whole code, but this part should be after your all filters, after ->get()
->transform(function ($item) use ($keyword) {
$item->mark_title = preg_replace('/('.$keyword.')/i', "<mark></mark>", $item->title);
return $item;
});
Please or to participate in this conversation.