从数据库导出到 xls (laravel 5)


Exporting from database to xls (laravel 5)

public function export(){
    view('tester');
    $assignments = DB::table('assignments')
        ->join('projects', 'assignments.project_id', '=', 'projects.id')
        ->join('people', 'assignments.person_id', '=', 'people.id')
        ->join('tasks', 'assignments.id', '=', 'tasks.assignment_id')
        ->select('assignments.*', 'projects.name','people.firstname','people.lastname', 'tasks.description','tasks.hours_spent')
        ->get();
    Excel::create('projects', function($excel) use($assignments) {
        $excel->sheet('Sheet 1', function($sheet) use($assignments) {
            $sheet->fromArray($assignments);
        });
    })->export('xls');

}

当我单击导出到 xls 时

    <form class="form-horizontal" role="form" method="POST" action="{{action('ReportController@export')}}">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}">  
                        <h4>Task Report</h4>
                        <button type="submit" class="btn btn-info btn-sm pull-right" style="margin-right: 10px">
                            Export to XLS format
                        </button>

错误是DefaultValueBinder 中的 ErrorException.php第 65 行:类 stdClass 的对象无法转换为字符串。请帮助我如何解决此问题。

试试这个简单易行:-

$assignments = json_decode( json_encode($assignments), true);
$filename   = "Report-".date('d-m-Y').'_'.time();
Excel::create($filename, function($excel)use($record)  
      {
        $excel->sheet('Sheet 1', function($sheet)use($record)
    {
  $sheet->loadView('view', compact(array('record')));
    $sheet->setOrientation('portrait');
    });
})->export('xls');
exit();

还可以在<table>中查看和显示记录,但此视图不调用任何布局部分

如果我使用数据库获取数据,我会收到错误....

DefaultValueBinder 中的 ErrorException.php第 65 行:类 stdClass 的对象无法转换为字符串

$ret = Excel::create('statsExtract', function($excel) {
    $data = DB::table('thrps')->get();
    $excel->sheet('stats', function($sheet) use($data) {
        $sheet->fromArray($data);
    });
    })->download('xls');

如果我使用雄辩模型提取.....它有效....

$ret = Excel::create('statsExtract', function($excel) {
    $data = Thrp::all();
    $excel->sheet('stats', function($sheet) use($data) {
        $sheet->fromArray($data);
    });
    })->download('xls');

但是,如果我尝试添加 ->leftJoin 语句,我会收到一个错误,即 leftJoin 不存在....