我将创建一个具有日期范围的报告文件。但是,我在将测试条目数组放置到Excel :: create()语句的位置上遇到了麻烦。我测试的内容尝试将$ posts设置为compact(),但是不起作用。
这是我的职责:
public function exportDatedPosts(Request $request)
{
$dateStart = $request->startDate;
$dateEnd = $request->endDate;
$posts = DB::table('posts')->whereBetween('created_at', [$dateStart, $dateEnd])->get();
Excel::create('PostsReport', function($excel)
{
$excel->sheet('New sheet', function($sheet)
{
$sheet->loadView('ExImports.DatedPost');
});
})->export('csv');
}
答案 0 :(得分:0)
已经解决了问题。我只需要通过use($ posts)传递$ posts并将-> with(compact('posts'))添加到loadView
public function exportDatedPosts(Request $request)
{
$dateStart = $request->startDate;
$dateEnd = $request->endDate;
$posts = DB::table('posts')
->distinct()
->leftJoin('post_tag', 'posts.id', '=', 'post_tag.post_id')
->leftJoin('tags', 'tags.id', '=', 'post_tag.tag_id')
->leftJoin('users', 'users.id', '=', 'posts.user_id')
->select('posts.title', 'posts.body' ,'users.name as user')
->whereBetween('posts.created_at', [$dateStart, $dateEnd])
->get();
Excel::create('PostsReport', function($excel) use($posts)
{
$excel->sheet('New sheet', function($sheet) use($posts)
{
$sheet->loadView('ExImports.DatedPost')->with(compact('posts'));
});
})->export('csv');
}