• 24-08-2024, 10:51:26
    #1
    <?php
    
    namespace App\Http\Controllers;
    
    use App\Models\Book;
    use App\Models\Record;
    use Illuminate\Http\Request;
    
    class WebController extends Controller
    {
      public function index()
      {
        $books = Book::query()->get();
    
        return view('home', compact("books"));
      }
    
      public function page(Request $request)
      {
        $name = $request->name;
        $books = Book::query()->get();
    
        if (view()->exists('pages.' . $name)){
          return view('pages.' . $name, compact("books"));
        } else {
          abort(404, 'Whatever you were looking for, look somewhere else');
        }
      }
      public function getRecords(Request $request)
      {
        $limit = $request->limit ?? 40;
        $lastID = $request->last_id ?? 0;
        $books = $request->books ?? [];
        $search = $request->search ?? "";
    
        $records = Record::query()
          ->with("book")
          ->orderBy("id", "ASC")
          ->limit($limit);
    
        $records->whereIn("kitap_id", $books);
    
        if (!empty($search)) {
          $records->where(function ($query) use ($search) {
            $query->where('urun_kodu', 'LIKE', "%{$search}%")
              ->orWhere('eski_urun_kodu', 'LIKE', "%{$search}%")
              ->orWhere('tanim', 'LIKE', "%{$search}%");
          });
        }
    
        if ($lastID) {
          $records->where("id", ">", $lastID);
        }
    
        $records = $records->get();
    
        return view('components.table.rows', compact("records"));
      }
    
      public function detail(Request $request)
      {
        $slug = $request->slug;
        $record = Record::query()
          ->with("book")
          ->where("slug", $slug)
          ->first();
    
        $lastRecords = Record::query()
          ->with("book")
          ->where("id", ">", $record->id)
          ->orderBy("id", "ASC")
          ->limit(15)
          ->get();
    
        $books = Book::query()->get();
    
        return view('detailV2', compact("record", "lastRecords", "books"));
      }
    }
    merhaba, yukarıdaki kodda tek sorunum şu. bütün türkçe karakterlerde hiç bir sorun yaşamama rağmen ı harfinde sorun yaşıyorum arama sonuçlarını gösterirken. örneğin HAZIR BETON şeklinde aratma yaptığımda hazır betonu getirmiyor ya da hazır beton olarak aratma yaptığımda HAZIR BETON'u getirmiyor. yani I ve ı harflerinde sorunum var. chatgpt ile de bayağı boğuştum ama sonuç alamadım. yardımcı olabilirseniz sevinirim.
  • 31-08-2024, 02:50:04
    #2
    kodlamada problem yok, mysql karakter setinde problem olabilir. yada kökten çözüm için aramalar için full-text search e dönebilirsin, laravel için nette örnekleri bolca mevcut
  • 05-09-2024, 23:35:50
    #3
    TurkishSearchable isminde bir trait oluşturun ve ardındanbu traiti model içerisinde kullanın. Büyük küçük harf hatanızı giderecektir.

    app/Traits/TurkishSearchable.php
    <?php
    
    namespace App\Traits;
    
    use Illuminate\Database\Eloquent\Builder;
    
    
    trait TurkishSearchable
    {
        public function scopeTurkishSearch(Builder $query, $column, $searchTerm)
        {
            $searchTerm = $this->normalizeTurkishString($searchTerm);
            return $query->where(function ($q) use ($column, $searchTerm) {
                $q->whereRaw("LOWER($column) LIKE ?", ['%' . $searchTerm . '%']);
            });
        }
    
        protected function normalizeTurkishString($string)
        {
            $replacements = [
                'ı' => 'i', 'İ' => 'i', 'ş' => 's', 'Ş' => 's',
                'ç' => 'c', 'Ç' => 'c', 'ğ' => 'g', 'Ğ' => 'g',
                'ü' => 'u', 'Ü' => 'u', 'ö' => 'o', 'Ö' => 'o'
            ];
            return strtr(mb_strtolower($string), $replacements);
        }
    }
    app/Models/TestModel.php
    <?php
    
    namespace App\Models;
    
    use App\Traits\TurkishSearchable;
    use Wildside\Userstamps\Userstamps;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\SoftDeletes;
    
    
    class TestModel extends Model
    {
        use SoftDeletes, Userstamps, TurkishSearchable;
        protected $fillable = [
            'is_active',
            'content',
        ];
    }
  • 13-09-2024, 16:41:48
    #4
    config/database.php
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',