2026-01-24 14:03:11 +00:00
|
|
|
<?php
|
|
|
|
|
namespace App\DB\Te;
|
2026-09-01 21:38:09 +03:30
|
|
|
|
|
|
|
|
use App\Support\PersianText;
|
|
|
|
|
|
2026-01-24 14:03:11 +00:00
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: AraNote
|
|
|
|
|
* Date: 12/04/2018
|
|
|
|
|
* Time: 10:32 PM
|
|
|
|
|
*/
|
|
|
|
|
class Search
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
protected $cols, $text, $query;
|
|
|
|
|
|
|
|
|
|
public static function query(&$query, $cols, $search)
|
|
|
|
|
{
|
2026-09-01 21:38:09 +03:30
|
|
|
$like = PersianText::likePattern(strtolower((string) $search));
|
|
|
|
|
$query->where(function ($q) use ($cols, $like) {
|
2026-01-24 14:03:11 +00:00
|
|
|
$first = true;
|
|
|
|
|
foreach ($cols as $col) {
|
|
|
|
|
if (self::hasJoin($col)) {
|
|
|
|
|
$exp = explode('.', $col);
|
|
|
|
|
if (sizeof($exp) > 2) {
|
|
|
|
|
if ($first){
|
|
|
|
|
$whereHas = 'whereHas';
|
|
|
|
|
}else{
|
|
|
|
|
$whereHas = 'orWhereHas';
|
|
|
|
|
}
|
|
|
|
|
$q->$whereHas
|
2026-09-01 21:38:09 +03:30
|
|
|
($exp[0], function ($q) use ($exp, $like) {
|
|
|
|
|
return $q->whereHas($exp[1], function ($q2) use ($exp, $like) {
|
|
|
|
|
return $q2->whereRaw(self::foldedLikeSql($exp[2]), [$like]);
|
2026-01-24 14:03:11 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
if ($first){
|
|
|
|
|
$whereHas1 = 'whereHas';
|
|
|
|
|
}else{
|
|
|
|
|
$whereHas1 = 'orWhereHas';
|
|
|
|
|
}
|
|
|
|
|
$q->$whereHas1
|
2026-09-01 21:38:09 +03:30
|
|
|
($exp[0], function ($q) use ($exp, $like) {
|
|
|
|
|
return $q->whereRaw(self::foldedLikeSql($exp[1]), [$like]);
|
2026-01-24 14:03:11 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if ($first){
|
|
|
|
|
$where = 'whereRaw';
|
|
|
|
|
}else{
|
|
|
|
|
$where = 'orWhereRaw';
|
|
|
|
|
}
|
2026-09-01 21:38:09 +03:30
|
|
|
$q->$where(self::foldedLikeSql($col), [$like]);
|
2026-01-24 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
if ($first) {
|
|
|
|
|
$first = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-01 21:38:09 +03:30
|
|
|
private static function foldedLikeSql(string $column): string
|
|
|
|
|
{
|
|
|
|
|
return PersianText::sqlFoldExpr('lower(`'.$column.'`)').' like ?';
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 14:03:11 +00:00
|
|
|
public static function hasJoin($col)
|
|
|
|
|
{
|
|
|
|
|
return sizeof(explode('.', $col)) > 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|