【问题标题】:Export data with parameter year and month Laravel + Mysql带参数年月导出数据 Laravel + Mysql
【发布时间】:2020-08-31 02:46:14
【问题描述】:

当我想用参数实现导出数据时,我得到了堆栈,这是来自控制器的代码

public function export_byMonth(Request $request)
{
    return Excel::download(new ProjectMSelected($request->m , $request->y), 'ProjectMonthly_'.$request->m.'_'.$request->y.'.xlsx');
}

注意 M 是月,Y = 是年

这是使用 maatwebsite/excel 导出的代码。

namespace App\Exports;

use App\table2;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;

class ProjectMSelected implements FromView, WithHeadings
{
    use Exportable;

  public function __construct($m , $y)
  {
      $this->m = $m;
      $this->y = $y;
  }
  public function headings(): array
  {
   return [
      'Code Project' ,
      'Name',
      'Directorates',
      'Division', 
      'Scope',
      'Priority',
      'Progress',
      '%',
      'Remarks',
      'Plan',
      'pdate',
      'PM',
      'CO PM',
  ];
}
public function view(): View
{
    return view('report.excel.report_monthly_per_project', [
        'project_array' => table2::whereRaw('YEAR(tgl_masuk) = ',$this->y ,'And Month(tgl_masuk) =', $this->m)->get()
    ]);
}
}

还是这样解析错误信息,如何解决?

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 (SQL: select * from `projectmonthlyview2` where Month(tgl_masuk) = YEAR(tgl_masuk) =)

【问题讨论】:

    标签: mysql laravel laravel-5 eloquent


    【解决方案1】:

    如何改用这个:

    table2::whereRaw('YEAR(tgl_masuk) = ' . $this->y . ' AND MONTH(tgl_masuk) = ' . $this->m)->get()
    

    【讨论】:

    • 仍然收到错误消息,语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的“和 MONTH(tgl_masuk) =”附近使用正确的语法(SQL:select * from projectmonthlyview2 where YEAR(tgl_masuk) = And MONTH(tgl_masuk) =)
    • 你能确定 $request->y 和 $request->m 不是空值吗?
    猜你喜欢
    • 2019-11-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 2023-03-15
    • 2012-12-07
    相关资源
    最近更新 更多