Laravel DB 연동 검색어 자동완성

아기코딩단2·2023년 6월 22일
0
post-thumbnail

개뻘짓 하루종일 하고 간단하게 구현했다.

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.7.0.js" integrity="sha256-JlqSTELeR4TLqP0OG9dxM7yDPqX1ox/HfgiSLBj8+kM=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

이거 세개 무조건 필요함 없으면 이상하게 나오거나 에러뜸

html 코드

        <div class="mb-3">
            <label for="flow-manager" class="form-label">담당자</label>
            <input type="text" name="flow-manager" class="form-control" id="flow-manager" autocomplete="" placeholder="" >

        </div>

자바스크립트 코드(제이쿼리)

$(function() {
    $('#flow-manager').autocomplete({
        source: '{{ route('admin.pages.userAjax') }}',
        minLength: 1, // 최소 입력 글자 수
    });
});

jquery 코드는 이렇게 짜면됨

php 코드

라우트 코드는 생략함

public function userAjax(Request $request)
    {
        $term = $request->input('term'); //사용자 입력값
        $users = User::where('name', 'LIKE', '%' . $term . '%')->pluck('name');
        return response()->json($users);
    }
    

DB 에 박길동 두명들어있음

도움됐다면 댓글과 하트 부탁드림니다.

profile
레거시 학살자

0개의 댓글