I have this query that returns the Products that contains the search
word. How can i sort them based on the number of occurrences of the search
word ?
$products = Product::where('name','LIKE','%'.$search.'%')
->orWhere('name', 'like', '%' . str_replace(' ', '-', $search) . '%')
->orWhere('features','LIKE','%'.$search.'%')->get();
I have the SQL
query but i don't know how can i combine it with the query above :
SELECT *,
(LENGTH(`name`) - LENGTH(REPLACE(`name`, $search, ''))) / LENGTH('test') `appears_in_text`,
(LENGTH(`features`) - LENGTH(REPLACE(`features`, $search, ''))) / LENGTH($search) `appears_in_subject`,
(LENGTH(CONCAT(`name`,' ',`features`)) - LENGTH(REPLACE(CONCAT(`name`,' ',`features`), $search, ''))) / LENGTH($search) `occurences`
FROM
`products`
WHERE (name LIKE $search OR features LIKE $search)
ORDER BY `occurences` DESC