Laravel日期间搜索过滤器

时间:2019-04-15 06:48:27

标签: mysql laravel-5

我正在使用Laravel 5.8和mysql。我想使用字段created_at作为条件在日期之间创建搜索。当选择了StartDate和EndDate时,然后单击搜索,它将在GridView中显示结果。然后,单击“导出”,它将导出到Excel。默认情况下,它显示在GridView上。但是单击“搜索”和“导出”按钮后,什么也不会发生。

我创建了报表控制器和包含日期选择器的视图

Contoller

public function showServiceOverview()
{
    $serviceoverviews = ServicePackage::with('services')->paginate(15);

    $column = '';
    $string = '';
    $dateFrom = '';
    $dateTo = '';      

    return view('report.serviceoverview.show_service_overview', compact('serviceoverviews', 'column', 'string','dateFrom','dateTo'));         
}

public function searchServiceoverview(Request $request)
{

try
{
    $column = $request->column;
    $dateTo = $request->dateTo;
    $dateFrom = $request->dateFrom; 

    $data = ['created_at' => 'service_package.created_at', 'title' => 'service_package.title', 'descriptions' => 'service_package.descriptions', 'service_name' => 'services.name'];        

    if ($request->button == 'Search')   
    {
      $serviceoverviews = \DB::table('service_package')->select('service_package.id', 'service_package.title', 'service_package.descriptions', 'service_package.created_at', 'services.name')->join('services', 'service_package.service_id', '=', 'services.id');            

      if (!empty($column) && !empty($string) && empty($dateFrom) && empty($dateTo))
      {
        $serviceoverviews = $serviceoverviews->whereRaw($data[$column] . " like '%" . $string . "%' ")->paginate(20);
      }
      elseif (!empty($dateFrom) && !empty($dateTo) && empty($column) && empty($string))
      {
        $dateTo = date_format(date_create($request->dateTo), 'Y-m-d');
        $dateFrom = date_format(date_create($request->dateFrom), 'Y-m-d');
        $serviceoverviews = $serviceoverviews->whereBetween('created_at', [$dateFrom, $dateTo])->paginate(20);
      }
      elseif (!empty($column) && !empty($string) && !empty($dateFrom) && !empty($dateTo))
      {
        $dateTo = date_format(date_create($request->dateTo), 'Y-m-d');
        $dateFrom = date_format(date_create($request->dateFrom), 'Y-m-d');
        $serviceoverviews = $serviceoverviews->whereRaw($data[$column] . " like '%" . $string . "%'")->whereBetween('created_at', [$dateFrom, $dateTo])->paginate(20);
      }
      elseif (empty($column) && empty($string) && !empty($dateFrom) && !empty($dateTo))
      {
        $dateTo = date_format(date_create($request->dateTo), 'Y-m-d');
        $dateFrom = date_format(date_create($request->dateFrom), 'Y-m-d');
        $serviceoverviews = $serviceoverviews->whereRaw()->whereBetween('created_at', [$dateFrom, $dateTo])->paginate(20);
      }          
      else
      {
        $serviceoverviews = $serviceoverviews->paginate(20);
      }
      $post = 'post';

      return view('report.serviceoverview.show_service_overview', compact('serviceoverviews', 'post', 'column', 'string','dateFrom','dateTo'));          
    }
    else
    {
      /**
       * First we build a query string which is common in both cases whether we have a condition set or not
       */
      $serviceoverviews = \DB::table('service_package')->select('service_package.id', 'service_package.title', 'service_package.descriptions', 'service_package.created_at', 'services.name')->join('services', 'service_package.service_id', '=', 'services.id');

      if (!empty($column) && !empty($string) && empty($dateFrom) && empty($dateTo))
      {
        $serviceoverviews = $serviceoverviews->whereRaw($data[$column] . " like '%" . $string . "%' ")->get();
      }
      elseif (!empty($dateFrom) && !empty($dateTo) && empty($column) && empty($string))
      {
        $dateTo = date_format(date_create($request->dateTo), 'Y-m-d');
        $dateFrom = date_format(date_create($request->dateFrom), 'Y-m-d');
        $serviceoverviews = $serviceoverviews->whereBetween('date_from', [$dateFrom, $dateTo])->get();
      }
      elseif (!empty($column) && !empty($string) && !empty($dateFrom) && !empty($dateTo))
      {
        $dateTo = date_format(date_create($request->dateTo), 'Y-m-d');
        $dateFrom = date_format(date_create($request->dateFrom), 'Y-m-d');
        $serviceoverviews = $serviceoverviews->whereRaw($data[$column] . " like '%" . $string . "%'")->whereBetween('date_from', [$dateFrom, $dateTo])->get();
      }
      else
      {
        $serviceoverviews = $serviceoverviews->get();
      }

      $fileName = 'Service_Overview_' . rand(1, 1000) . '.csv';
      $filePath = storage_path('export/') . $fileName;
      $file = new \SplFileObject($filePath, "a");
      // Add header to csv file.
      $headers = ['id', 'title', 'descriptions', 'created_at', 'service_name'];
      $file->fputcsv($headers);
      $status = '';
      foreach ($leaves as $leave)
      {
        $file->fputcsv([$serviceoverviews->id, $serviceoverviews->title, $serviceoverviews->descriptions, $serviceoverviews->created_at, $serviceoverviews->service_name]);
      }

      return response()->download(storage_path('export/') . $fileName);            
    }

  } 
  catch (\Exception $e)
  {
    return redirect()->back()->with('message', $e->getMessage());
  }            
}

查看

public function searchServiceoverview(Request $request)
{

try
{
    $column = $request->column;
    $dateTo = $request->dateTo;
    $dateFrom = $request->dateFrom; 

    $data = ['created_at' => 'service_package.created_at', 'title' => 'service_package.title', 'descriptions' => 'service_package.descriptions', 'service_name' => 'services.name'];        

    if ($request->button == 'Search')   
    {
      $serviceoverviews = \DB::table('service_package')->select('service_package.id', 'service_package.title', 'service_package.descriptions', 'service_package.created_at', 'services.name')->join('services', 'service_package.service_id', '=', 'services.id');            

      if (!empty($column) && !empty($string) && empty($dateFrom) && empty($dateTo))
      {
        $serviceoverviews = $serviceoverviews->whereRaw($data[$column] . " like '%" . $string . "%' ")->paginate(20);
      }
      elseif (!empty($dateFrom) && !empty($dateTo) && empty($column) && empty($string))
      {
        $dateTo = date_format(date_create($request->dateTo), 'Y-m-d');
        $dateFrom = date_format(date_create($request->dateFrom), 'Y-m-d');
        $serviceoverviews = $serviceoverviews->whereBetween('created_at', [$dateFrom, $dateTo])->paginate(20);
      }
      elseif (!empty($column) && !empty($string) && !empty($dateFrom) && !empty($dateTo))
      {
        $dateTo = date_format(date_create($request->dateTo), 'Y-m-d');
        $dateFrom = date_format(date_create($request->dateFrom), 'Y-m-d');
        $serviceoverviews = $serviceoverviews->whereRaw($data[$column] . " like '%" . $string . "%'")->whereBetween('created_at', [$dateFrom, $dateTo])->paginate(20);
      }
      elseif (empty($column) && empty($string) && !empty($dateFrom) && !empty($dateTo))
      {
        $dateTo = date_format(date_create($request->dateTo), 'Y-m-d');
        $dateFrom = date_format(date_create($request->dateFrom), 'Y-m-d');
        $serviceoverviews = $serviceoverviews->whereRaw()->whereBetween('created_at', [$dateFrom, $dateTo])->paginate(20);
      }          
      else
      {
        $serviceoverviews = $serviceoverviews->paginate(20);
      }
      $post = 'post';

      return view('report.serviceoverview.show_service_overview', compact('serviceoverviews', 'post', 'column', 'string','dateFrom','dateTo'));          
    }
    else
    {
      /**
       * First we build a query string which is common in both cases whether we have a condition set or not
       */
      $serviceoverviews = \DB::table('service_package')->select('service_package.id', 'service_package.title', 'service_package.descriptions', 'service_package.created_at', 'services.name')->join('services', 'service_package.service_id', '=', 'services.id');

      if (!empty($column) && !empty($string) && empty($dateFrom) && empty($dateTo))
      {
        $serviceoverviews = $serviceoverviews->whereRaw($data[$column] . " like '%" . $string . "%' ")->get();
      }
      elseif (!empty($dateFrom) && !empty($dateTo) && empty($column) && empty($string))
      {
        $dateTo = date_format(date_create($request->dateTo), 'Y-m-d');
        $dateFrom = date_format(date_create($request->dateFrom), 'Y-m-d');
        $serviceoverviews = $serviceoverviews->whereBetween('date_from', [$dateFrom, $dateTo])->get();
      }
      elseif (!empty($column) && !empty($string) && !empty($dateFrom) && !empty($dateTo))
      {
        $dateTo = date_format(date_create($request->dateTo), 'Y-m-d');
        $dateFrom = date_format(date_create($request->dateFrom), 'Y-m-d');
        $serviceoverviews = $serviceoverviews->whereRaw($data[$column] . " like '%" . $string . "%'")->whereBetween('date_from', [$dateFrom, $dateTo])->get();
      }
      else
      {
        $serviceoverviews = $serviceoverviews->get();
      }

      $fileName = 'Service_Overview_' . rand(1, 1000) . '.csv';
      $filePath = storage_path('export/') . $fileName;
      $file = new \SplFileObject($filePath, "a");
      // Add header to csv file.
      $headers = ['id', 'title', 'descriptions', 'created_at', 'service_name'];
      $file->fputcsv($headers);
      $status = '';
      foreach ($leaves as $leave)
      {
        $file->fputcsv([$serviceoverviews->id, $serviceoverviews->title, $serviceoverviews->descriptions, $serviceoverviews->created_at, $serviceoverviews->service_name]);
      }

      return response()->download(storage_path('export/') . $fileName);            
    }

  } 
  catch (\Exception $e)
  {
    return redirect()->back()->with('message', $e->getMessage());
  }            
}

0 个答案:

没有答案