lundi 9 juillet 2018

Laravel 5 AJAX search

I know this question is duplicated but none of them solved my problem. I had an AJAX search which will display all the request of a specific client.

A client has many request it is a one to many relation.

Now this is I had in my routes.php

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

I had a resource route which take all 4 http request but the one I am targeting is the GET/specificResource with the id being passed in the params.

Now in my show method in ClientController.php

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'));
        }

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

Now my ajax script below.

@section('scripts')
<script>
  $('.searchbar').keypress(function (e) {
    if (e.which == 13) {
        $.ajax({
            dataType: "json",
            url = '' + $('#client_id').value(),
            method: 'GET'
            data: {keyword: $('.searchbar').value()},
            success: function (result) {
                    console.log(result);
            },
            error: function(){
                    console.log("No results for " + data + ".");
            }
        });
    }
});
</script>
@endsection

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" value="">
                            <input class="form-control" 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 had no error here I just can't figure out why.

Appreciate if someone could help. Thanks in advance.



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

Aucun commentaire:

Enregistrer un commentaire