使用Laravel和AJAX搜索两个表时出错

时间:2018-11-01 21:58:31

标签: php ajax laravel

使用Laravel和AJAX搜索两个表时出错 您好,我正在系统中开发一个搜索字段,它需要在两个表中搜索数据,但是我无法以任何方式尝试所有教程并检查了所有文档,但是找不到解决方案。 通过Jquery处理请求时,总是错误500。我相信错误在于负责请求的控制器中。 请遵循以下所有代码,谢谢您的回复。

路线

Route::get('/customers', 'HomeController@customers')->name('customers');
Route::get('/customers/action', 'HomeController@customersAction')->name('customers.action');

HomeController

    public function customers(Request $request)
{

    $data = DB::table('projects')
        ->join('projects_info', 'projects.id', '=', 'projects_info.project_id')
        ->select('projects.*', 'projects_info.cpf')
        ->orderBy('id', 'asc')
        ->paginate(3);

    $menuTasks['view_type'] = "overview";
     if ($request->ajax()) {
        return view('project.livepagination', compact('data'));
    }
    return view('customers', compact('data'), $menuTasks);
}

public function customersAction(Request $request)
{
if($request->ajax())
 {
  $output = '';
  $query = $request->get('query');
  if($query != '')
  {

    $data = DB::table('projects')
         ->join('projects_info', 'projects.id', '=', 'projects_info.project_id')
         ->select('projects.title as title','projects.id as id', 'projects.privacy as privacy', 'projects.user_id as user_id', 'projects_info.cpf as cpf')
            ->where('title', 'like', '%'.$query.'%')
            ->orWhere('id', 'like', '%'.$query.'%')
            ->orWhere('cpf', 'like', '%'.$query.'%')
            ->orWhere('privacy', 'like', '%'.$query.'%')
            ->orWhere('user_id', 'like', '%'.$query.'%')
            ->orderBy('id', 'asc')->get();

  }
  else
  {
   $data = DB::table('projects')
     ->join('projects_info', 'projects.id', '=', 'projects_info.project_id')
     ->select('projects.*', 'projects_info.cpf')
     ->orderBy('id', 'asc')
     ->paginate(3);
  }

  $total_row = $data->count();
  if($total_row > 0)
  {

   foreach($data as $row)
   {
    $output .= '
    <tr>
     <td>'.$row->title.'</td>
     <td>'.$row.'</td>
     <td>'.$row->id.'</td>
     <td>'.$row->privacy.'</td>
     <td>'.$row->user_id.'</td>
    </tr>
    ';
   }
  }
  else
  {
   $output = '
   <tr>
    <td align="center" colspan="5">Nenhum cadastro encontrado.</td>
   </tr>
   ';
  }
  $data = array(
   'table_data'  => $output,
   'total_data'  => $total_row
  );

  echo json_encode($data);
 }
}

Customer.blade

<div id="tag_container">
        @include('project.livepagination')
</div>

customer.blade(jquery-ajax)

//Pagination
$(window).on('hashchange', function() {
        if (window.location.hash) {
            var page = window.location.hash.replace('#', '');
            if (page == Number.NaN || page <= 0) {
                return false;
            }else{
                getData(page);
            }
        }
    });

$(document).ready(function()
{
     $(document).on('click', '.pagination a',function(event)
    {
        event.preventDefault();
        $('li').removeClass('active');
        $(this).parent('li').addClass('active');
        var myurl = $(this).attr('href');
        var page=$(this).attr('href').split('page=')[1];
        getData(page);
    });
});

function getData(page){
        $.ajax(
        {
            url: '?page=' + page,
            type: "get",
            datatype: "html"
        })
        .done(function(data)
        {
            $("#tag_container").empty().html(data);
            location.hash = page;
        })
        .fail(function(jqXHR, ajaxOptions, thrownError)
        {
              alert('Sem resposta do servidor!');
        });
}

$(document).on('keyup', '#search', function() {
$query = $(this).val().toLowerCase();
if ($query) {
    $.ajax({
        url: "{{ route('customers.action') }}",
        method: 'GET',
        data: {
            'query': $query
        },
        dataType: 'json',
        success: function(data) {
            $('tbody').html(data.table_data);
        }
    });
} else {
    $.ajax({
        url: "{{ route('customers.action') }}",
        method: 'GET',
        data: {
            'query': $query
        },
        dataType: 'json',
        success: function(data) {
            $('tbody').html(data.table_data);
        }
    });
}});

Project.livepagination

<div class="form-group">
    <input type="text" name="search" id="search" class="form-control" placeholder="Busca..."/>
    </div>
<div class="table-responsive">
  <table class="table table-striped table-bordered">
   <thead>
    <tr>
     <th>Nome</th>
     <th>ID</th>
     <th>CPF</th>
     <th>Privacy</th>
     <th>User_id</th>
    </tr>
   </thead>
   <tbody>
    @foreach ($data as $row)
    <tr>
        <td>{{$row->title}}</td>
        <td>{{$row->cpf}}</td>
        <td>{{$row->id}}</td>
        <td>{{$row->privacy}}</td>
        <td>{{$row->user_id}}</td>
    </tr>
    @endforeach
   </tbody>
  </table>
{!! $data->links() !!} 
</div>

模型/项目

public function projects_info() {
    return $this->hasOne('App\ProjectsInfo', 'id');
}

模型/ ProjectInfo

public function projects() {
return $this->belongsTo('App\Project', 'project_id');

使用AJAX的搜索系统中是否存在所有代码,希望对发现该错误有帮助。

1 个答案:

答案 0 :(得分:0)

这是需要它的人的正确代码:

HomeController

public function customersAction(Request $request)
{
if($request->ajax())
 {
  $output = '';
  $query = $request->get('query');
  if($query != '')
  {


    $data = DB::table('projects')
         ->join('projects_info', 'projects.id', '=', 'projects_info.project_id')
          ->select('projects.title as title', 'projects.id as id', 'projects.privacy as privacy', 'projects.user_id as user_id', 'projects_info.cpf as cpf', 'projects_info.project_id as project_id')
            ->where('title', 'like', '%'.$query.'%')
            ->orWhere('cpf', 'like', '%'.$query.'%')
            ->orWhere('project_id', 'like', '%'.$query.'%')
            ->orWhere('privacy', 'like', '%'.$query.'%')
            ->orWhere('user_id', 'like', '%'.$query.'%')
            ->orderBy('id', 'asc')->get();

  }
  else
  {
   $data = DB::table('projects')
     ->join('projects_info', 'projects.id', '=', 'projects_info.project_id')
     ->select('projects.title as title', 'projects.id as id', 'projects.privacy as privacy', 'projects.user_id as user_id', 'projects_info.cpf as cpf', 'projects_info.project_id as project_id')
     ->orderBy('id', 'asc')
     ->paginate(3);
  }

  $total_row = $data->count();
  if($total_row > 0)
  {

   foreach($data as $row)
   {
    $output .= '
    <tr>
     <td>'.$row->title.'</td>
     <td>'.$row->cpf.'</td> 
    <td>'.$row->project_id.'</td>
     <td>'.$row->privacy.'</td>
     <td>'.$row->user_id.'</td>
    </tr>
    ';
   }
  }
  else
  {
   $output = '
   <tr>
    <td align="center" colspan="5">Nenhum cadastro encontrado.</td>
   </tr>
   ';
  }
  $data = array(
   'table_data'  => $output,
   'total_data'  => $total_row
  );

  echo json_encode($data);
 }
}