• We just launched and are currently in beta. Join us as we build and grow the community.

Laravel: Search using AJAX

rebelstar

Doujin Anthology Creator
R Rep
0
0
0
Rep
0
R Vouches
0
0
0
Vouches
0
Posts
192
Likes
179
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Getting Started

First, we're going to create a new project and I'm gonna name it search and add it to localhost with the name search.dev. If you have no idea on how to do this, please refer to my tutorial Installing Laravel - PHP Framework.

Setting up our Database

1. Open your phpMyAdmin and create a new database. In my case, I've created a database named search.

2. In our project, open .env file and update the ff lines depending on your setting.

DB_DATABASE=search
DB_USERNAME=root
DB_PASSWORD=

Creating our Controller

1. In command prompt, navigate to your project and type:

php artisan make:controller MemberController

This will create our controller in the form of MemberController.php located in app/Http/Controllers folder.

2. Open MemberController.php and edit it with the ff codes:

  1. <?php

  2. namespace

    App\Http\Controllers;

  3. use

    Illuminate\Http\Request;
  4. use

    App\Member;

  5. class

    MemberController extends

    Controller
  6. {
  7. public

    function

    index(

    )

    {
  8. $members

    =

    Member::

    all

    (

    )

    ;
  9. return

    view(

    'search'

    )

    ->

    with

    (

    'members'

    ,

    $members

    )

    ;
  10. }

  11. public

    function

    search(

    Request $request

    )

    {
  12. $search

    =

    $request

    ->

    input

    (

    'search'

    )

    ;

  13. $members

    =

    Member::

    where

    (

    'firstname'

    ,

    'like'

    ,

    "$search

    %"

    )
  14. ->

    orWhere

    (

    'lastname'

    ,

    'like'

    ,

    "$search

    %"

    )
  15. ->

    get

    (

    )

    ;

  16. return

    view(

    'result'

    )

    ->

    with

    (

    'members'

    ,

    $members

    )

    ;
  17. }

  18. public

    function

    viewmember(

    $id

    )

    {

  19. $member

    =

    Member::

    find

    (

    $id

    )

    ;

  20. return

    view(

    'member'

    )

    ->

    with

    (

    'member'

    ,

    $member

    )

    ;
  21. }

  22. public

    function

    find(

    Request $request

    )

    {
  23. $search

    =

    $request

    ->

    input

    (

    'search'

    )

    ;

  24. $members

    =

    Member::

    where

    (

    'firstname'

    ,

    'like'

    ,

    "$search

    %"

    )
  25. ->

    orWhere

    (

    'lastname'

    ,

    'like'

    ,

    "$search

    %"

    )
  26. ->

    get

    (

    )

    ;

  27. return

    view(

    'searchresult'

    )

    ->

    with

    (

    'members'

    ,

    $members

    )

    ;
  28. }
  29. }

Creating our Model

1. In command prompt, navigate to our project and type:

php artisan make:model Member -m

This will create our model Member.php located in app folder. It will also create the migration for us due to the -m that we added in creating the model in the form of, in my case 2017_11_22_032430_create_members_table.php file located in database/migrations folder.

2. Open 2017_11_22_032430_create_members_table.php and edit it with ff codes:

  1. <?php

  2. use

    Illuminate\Support\Facades\Schema;
  3. use

    Illuminate\Database\Schema\Blueprint;
  4. use

    Illuminate\Database\Migrations\Migration;

  5. class

    CreateMembersTable extends

    Migration
  6. {
  7. /**
  8. * Run the migrations.
  9. *
  10. * @return void
  11. */
  12. public

    function

    up(

    )
  13. {
  14. Schema::

    create

    (

    'members'

    ,

    function

    (

    Blueprint $table

    )

    {
  15. $table

    ->

    increments

    (

    'id'

    )

    ;
  16. $table

    ->

    string

    (

    'firstname'

    )

    ;
  17. $table

    ->

    string

    (

    'lastname'

    )

    ;
  18. $table

    ->

    timestamps

    (

    )

    ;
  19. }

    )

    ;
  20. }

  21. /**
  22. * Reverse the migrations.
  23. *
  24. * @return void
  25. */
  26. public

    function

    down(

    )
  27. {
  28. Schema::

    dropIfExists

    (

    'members'

    )

    ;
  29. }
  30. }

Migrating

In command prompt, navigate to your project and type: php artisan migrate

It will then create our database migration.

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was t
oo long; max key length is 767 bytes (SQL: alter table `users` add unique `
users_email_unique`(`email`))

You can solve this by opening AppServiceProvider.php located in app/Providers folder.

Add this line: use Illuminate\Support\Facades\Schema;

In boot add this line: Schema::defaultStringLength(191);
migrateerror2_0.png


Run php artisan migrate again and make sure that your database is empty because it will have another error if its not.

Creating our Routes

In routes folder, open web.php and edit it with the ff codes:

  1. <?php
  2. Route::

    get

    (

    '/'

    ,

    'MemberController@index'

    )

    ;

  3. Route::

    get

    (

    '/search'

    ,

    'MemberController@search'

    )

    ;

  4. Route::

    get

    (

    '/member/{id}'

    ,

    'MemberController@viewmember'

    )

    ;

  5. Route::

    post

    (

    '/find'

    ,

    'MemberController@find'

    )

    ;

Creating our Views

In resources/views folder, create the ff files:

app.blade.php
  1. <!

    DOCTYPE html>
  2. <

    html>
  3. <

    head>
  4. <

    meta charset=

    "UTF-8"

    >
  5. <

    meta name=

    "csrf-token"

    content=

    "{{ csrf_token() }}"

    >
  6. <

    title>

    Laravel:

    Search using AJAX</

    title>
  7. <

    link

    rel=

    "stylesheet"

    href=

    "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"

    />
  8. <

    link

    rel=

    "stylesheet"

    href=

    "/css/app.css"

    >
  9. <

    script src=

    "https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"

    >

    </script>
  10. <

    script src=

    "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"

    >

    </script>
  11. </

    head>
  12. <

    body>
  13. @

    include

    (

    'navbar'

    )

  14. <

    div id=

    "result"

    class

    =

    "panel panel-default"

    style=

    "width:400px; position:absolute; left:180px; top:55px; z-index:1; display:none"

    >
  15. <

    ul style=

    "margin-top:10px; list-style-type:none;"

    id=

    "memList"

    >

  16. </

    ul>
  17. </

    div>
  18. <

    div class

    =

    "container"

    >
  19. @

    yield

    (

    'content'

    )

  20. </

    div>
  21. <

    script type=

    "text/javascript"

    >
  22. $(

    document)

    .

    ready(

    function

    (

    )

    {
  23. $.

    ajaxSetup(

    {
  24. headers:

    {
  25. 'X-CSRF-TOKEN'

    :

    $(

    'meta[name="csrf-token"]'

    )

    .

    attr(

    'content'

    )
  26. }
  27. }

    )

    ;

  28. $(

    '#search'

    )

    .

    keyup(

    function

    (

    )

    {
  29. var

    search =

    $(

    '#search'

    )

    .

    val(

    )

    ;
  30. if

    (

    search==

    ""

    )

    {
  31. $(

    "#memList"

    )

    .

    html(

    ""

    )

    ;
  32. $(

    '#result'

    )

    .

    hide(

    )

    ;
  33. }
  34. else

    {
  35. $.

    get(

    "{{ URL::to('search') }}"

    ,

    {

    search:

    search}

    ,

    function

    (

    data)

    {
  36. $(

    '#memList'

    )

    .

    empty

    (

    )

    .

    html(

    data)

    ;
  37. $(

    '#result'

    )

    .

    show(

    )

    ;
  38. }

    )
  39. }
  40. }

    )

    ;
  41. }

    )

    ;
  42. </script>
  43. </

    body>
  44. </

    html>

narbar.blade.php
  1. <nav class

    =

    "navbar navbar-default"

    >
  2. <div

    class

    =

    "container-fluid"

    >
  3. <!-- Brand and toggle get grouped for better mobile display -->
  4. <div

    class

    =

    "navbar-header"

    >
  5. <button

    type

    =

    "button"

    class

    =

    "navbar-toggle collapsed"

    data-toggle=

    "collapse"

    data-target

    =

    "#bs-example-navbar-collapse-1"

    aria-expanded=

    "false"

    >
  6. <span

    class

    =

    "sr-only"

    >

    Toggle navigation</

    span

    >
  7. <span

    class

    =

    "icon-bar"

    ></

    span

    >
  8. <span

    class

    =

    "icon-bar"

    ></

    span

    >
  9. <span

    class

    =

    "icon-bar"

    ></

    span

    >
  10. </

    button

    >
  11. <a

    class

    =

    "navbar-brand"

    href

    =

    "#"

    >

    SourceCodester</

    a

    >
  12. </

    div

    >

  13. <!-- Collect the nav links, forms, and other content for toggling -->
  14. <div

    class

    =

    "collapse navbar-collapse"

    id

    =

    "bs-example-navbar-collapse-1"

    >
  15. <form

    class

    =

    "navbar-form navbar-left"

    action

    =

    "{{ URL::to('find') }}"

    method

    =

    "POST"

    >
  16. {{ csrf_field() }}
  17. <div

    class

    =

    "input-group"

    >
  18. <input

    type

    =

    "text"

    id

    =

    "search"

    name

    =

    "search"

    class

    =

    "form-control"

    placeholder=

    "Search Member"

    >
  19. <span

    class

    =

    "input-group-btn"

    >
  20. <button

    type

    =

    "submit"

    class

    =

    "btn btn-default"

    >
  21. <span

    class

    =

    "glyphicon glyphicon-search"

    ></

    span

    >
  22. </

    button

    >
  23. </

    span

    >
  24. </

    div

    >
  25. </

    form

    >
  26. </

    div

    >

    <!-- /.navbar-collapse -->
  27. </

    div

    >

    <!-- /.container-fluid -->
  28. </

    nav>

search.blade.php
  1. @

    extends

    (

    'app'

    )

  2. @

    section(

    'content'

    )
  3. <

    div class

    =

    "row"

    >
  4. <

    div class

    =

    "col-md-8 col-md-offset-2"

    >
  5. <

    h2 class

    =

    "page-header text-center"

    >

    Search using AJAX</

    h2>
  6. <

    h4>

    Members Table</

    h4>
  7. <

    table class

    =

    "table table-bordered table-striped"

    >
  8. <

    thead>
  9. <

    th>

    Firstname</

    th>
  10. <

    th>

    Lastname</

    th>
  11. </

    thead>
  12. <

    tbody>
  13. @

    foreach

    (

    $members

    as

    $member

    )
  14. <

    tr>
  15. <

    td>

    {

    {

    $member

    ->

    firstname

    }

    }

    </

    td>
  16. <

    td>

    {

    {

    $member

    ->

    lastname

    }

    }

    </

    td>
  17. </

    tr>
  18. @

    endforeach
  19. </

    tbody>
  20. </

    table>
  21. </

    div>
  22. </

    div>
  23. @

    endsection

member.blade.php
  1. @

    extends

    (

    'app'

    )

  2. @

    section(

    'content'

    )
  3. <

    div class

    =

    "row"

    >
  4. <

    h2>

    {

    {

    $member

    ->

    firstname

    }

    }

    {

    {

    $member

    ->

    lastname

    }

    }

    </

    h2>
  5. <

    a href=

    "/"

    class

    =

    "btn btn-primary"

    ><

    span class

    =

    "glyphicon glyphicon-arrow-left"

    ></

    span>

    Home</

    a>
  6. </

    div>
  7. @

    endsection

searchresult.blade.php
  1. @

    extends

    (

    'app'

    )

  2. @

    section(

    'content'

    )
  3. @

    if

    (

    count

    (

    $members

    )

    >

    0

    )
  4. <

    h2>

    Search Results</

    h2>
  5. <

    ul style=

    "list-style-type:none;"

    >
  6. @

    foreach

    (

    $members

    as

    $member

    )
  7. <

    li><

    a href=

    "{{ url('member/'.$member->id

    ) }}"

    >

    {

    {

    $member

    ->

    firstname

    }

    }

    {

    {

    $member

    ->

    lastname

    }

    }

    </

    a></

    li>
  8. @

    endforeach
  9. </

    ul>
  10. @

    else
  11. <

    h2>

    No Results Found</

    h2>
  12. @

    endif
  13. @

    endsection

result.blade.php
  1. @

    if

    (

    count

    (

    $members

    )

    >

    0

    )
  2. @

    foreach

    (

    $members

    as

    $member

    )
  3. <

    li><

    a href=

    "{{ url('member/'.$member->id

    ) }}"

    >

    {

    {

    $member

    ->

    firstname

    }

    }

    {

    {

    $member

    ->

    lastname

    }

    }

    </

    a></

    li>
  4. @

    endforeach
  5. @

    else
  6. <

    li>

    No Results Found</

    li>
  7. @

    endif

Running our Server

In your web browser, type the name that you added in localhost for your project in my case, search.dev.

That ends this tutorial. Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top