mardi 10 juillet 2018

Laravel 5.5 AJAX live data search won't work

I have a AJAX search in my blade files the show.blade.php and I am dislaying a specific user in that show method like this in the url: http://127.0.0.1:8000/client/1

Here is my route

Route::resource('client', 'ClientController',
        ['as' => 'encoder']);

It is a resouce controller that includes the show method to display a specific client.

This is what I did in my controller

public function show($id, Request $request)
    {
        $client = $this->clientRepository->with(['request' => function ($query) {
            $query->orderBy('created_at', 'desc');
        }])->findWithoutFail($id);

        $keyword = $request->get('keyword');

        if (!$keyword) {
            $client_requests = AnalysisRequest::where('client_id', $client->id)
            ->orderBy('created_at', 'desc')->get();
        } else {
            $client_requests = AnalysisRequest::where('client_id', $client->id)
            ->OrWhere('id', 'LIKE', '%keyword%')
            ->OrWhere('sample_descrition', 'LIKE', '%keyword%')
            ->OrWhere('special_instruction', 'LIKE', '%keyword%')
            ->orderBy('created_at', 'desc')->get();
        }

        // dd($client_requests);

        if (empty($client)) {
            Flash::error('Client not found');

            return redirect(route('encoder.client.index'));
        }

        echo view('encoder-dashboard.client.show', compact('client_requests'))
        ->with('client', $client)
        ->render();
    }

I have a show function that will display a specific client that the id is being passed as a params in the controller in order to show that specific client. Now I also have a request for that specific client that I want to search with.

This is what I have done in my AJAX.

$(document).ready(function(){
      $('.searchbar').on('keyup', function(){
          var text = $('#searchbar').val();
          $.ajax({
              dataType: "json",
              type:"GET",
              url: '' + '/' + $('.id_search').val(),
              data: {text: $('.searchbar').val()},
              success: function(response) {
                console.log(response);
              }
          });
      });
  });

and in my show.blade.php

<div class="row">
                    <div class="col-sm-6">
                        <div class="form-group" id="results">
                            <input type="hidden" id="client_id" class="id_search" name="client_id" value="">
                            <input class="form-control searchbar" id="searchbar" name="searchbar" placeholder="Search...">
                        </div>
                    </div>
                </div>

                @include('encoder-dashboard.client.request')

and finally the request.blade.php

<!-- The timeline -->
  @if (isset($client_requests) && count($client_requests) > 0)
  @foreach($client_requests as $request)
  <ul class="timeline timeline-inverse">
    <!-- timeline time label -->
    <li class="time-label">
          <span class="bg-red">
            
          </span>
    </li>
    <!-- /.timeline-label -->
    <!-- timeline item -->
    <li>
      <i class="fa fa-edit bg-blue"></i>

      <div class="timeline-item">
        <span class="time"><i class="fa fa-clock-o"></i> </span>

        <h3 class="timeline-header">Request Code: <a href="{!! route('encoder.analysis-request.show', $request->id) !!}"></a>
            @if ($request->rushable == 1)
              <p style="color:red">This request is for RUSH!</p>
            @else
            @endif
        </h3>

        <div class="timeline-body">
          Description: <b></b>
          <br>
          Service Requested: <b></b>
          <br>
          Category Requested: <b></b>
          <br>
          Method Requested: <b></b>
          <br>
          Special Instruction: <b></b>
          <br>
          @if ($request->status == 'for_testing')
              Status: <span class="label label-warning">Pending</span>
          @elseif ($request->status == 'under_analyzation')
              Status: <span class="label label-info">Under Analyzation</span>
          @elseif ($request->status == 'finished')
              Status: <span class="label label-success">Finished</span>
          @endif
        </div>
        <div class="timeline-footer">
          <a class="btn btn-primary btn-xs" href="{!! route('encoder.analysis-request.show', $request->id) !!}">Read more</a>
          
        </div>
      </div>
    </li>
  </ul>
  @endforeach
  @endif

I am doing something wrong? I didn't receive any errors in here right now.

I have an 500 internal server error earlier but manage to fix it because of just a wrong url.

Maybe I should do I POST request instead of a GET request? But How? because I am also displaying a specific resource passing the id as a params.

Appreciate if someone could help. Thanks in advance.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2ujjLI0
via IFTTT

Aucun commentaire:

Enregistrer un commentaire