I want to iterate through list of words broken down in a function processNeedle. This is working fine with ordinary php but not in laravel.
$query = $request->input('query');
$trim = new SearchTrim();
$words = $trim->ProcessNeedle($query);
$concat = "CONCAT(";
$concat.="title,";
$concat.="'')";
$sql =DB::select("SELECT DISTINCT id,title,code,abstract FROM projects WHERE 0 ";
foreach ($words as $word) $sql.=" OR $concat LIKE '%$word%'";
$sql.=" ORDER BY id DESC";
My query function well like this in php
SQL query: SELECT DISTINCT id,title,code FROM projects WHERE 0 OR CONCAT(title,'') LIKE '%intranet%' OR CONCAT(title,'') LIKE '%mailing%' ORDER BY id DESC;
How do i achieve this in Laravel Please help
Try this example. i hope it helps you.
Pass foreach inside laravel DB::select query
$items = ['condition1', 'condition2', 'condition3']; $results = App\Model::where(function ($query) use ($items) { foreach($items as $item) { $query->orWhere('dbfield', 'LIKE', "%$item%"); } }) ->get(); dd($results);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.