<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;

class SearchController extends Controller
{
    public function index(Request $request)
    {
        $keyword = $request->k;
        $products = Product::query()->where(function ($query) use ($keyword) {
            $query->orWhere('jianjie1', 'like', "%$keyword%")
                ->orWhere('jianjie2', 'like', "%$keyword%")
                ->orWhere('pcode', 'like', "%$keyword%")
                ->orWhere('okpcode', 'like', "%$keyword%")
                ->orWhere('rep', 'like', "%$keyword%")
                ->orWhere('comp', 'like', "%$keyword%")
                ->orWhere('cs', 'like', "%$keyword%")
                ->orWhere('dl', 'like', "%$keyword%")
                ->orWhere('dy', 'like', "%$keyword%");
        })->where('stock', 'ok')->orderBy('pid', 'desc')->paginate(28);
        $products->appends($request->all());
        return view('search.index', compact('products', 'keyword'));
    }
}
