lundi 28 novembre 2016

Laravel-Excel for a Datatable and Images

What I am looking to reach is to have an excel document showing 3 columns and in the last one a list of images.

For example :

Page Title | Questions | Questions Status | Images (1,2,3)

What I've done till now is the following which is returning an empty excel !

$elements = DB::table('tour_manager')
            ->join('tours', 'tours.id', '=', 'tour_manager.tour_id')
            ->join('tour_questions', 'tour_questions.id', '=', 'tour_manager.question_id')
            ->join('tour_pages', 'tour_pages.id', '=', 'tour_questions.page_id')
            ->join('tour_manager_page_pics', 'tour_manager_page_pics.page_id', '=', 'tour_pages.id')
            ->where('tour_manager.tour_id', $tour)
            ->select('tours.status as tour_status', 'tours.pourcentConforme as pc', 'tours.id as tour_id',
                'tour_questions.title as question', 'tour_questions.id as question_id',
                'tour_manager_page_pics.imagePath1', 'tour_manager_page_pics.imagePath2', 'tour_manager_page_pics.imagePath3',
                'tour_pages.title as page', 'tour_pages.id as page_id', 'tour_manager.status as qstatus')
            ->distinct()
            ->get();

        $fcList = array();
        foreach ($elements as $c) {
            $fcList[$c->page]['questions'][$c->question] =
                $c->question_id;
            $fcList[$c->page]['status'][$c->question] =
                $c->qstatus;
            $fcList[$c->page]['images']['image1'] = $c->imagePath1;
            $fcList[$c->page]['images']['image2'] = $c->imagePath2;
            $fcList[$c->page]['images']['image3'] = $c->imagePath3;
        }

        Excel::create('Tour du Manager - Excel', function($excel) use($tour,$fcList) {
            $i =1 ;
            $j =1 ;
            $excel->sheet($tour, function ($sheet) use ($i,$j,$fcList) {
                foreach ($fcList as $c) {
                    foreach ($c as $k=>$t) {
                        foreach ($t as $m=>$o) {
                            $k = array($k);
                            $i = $i + 5;
                            $j = $j ++;
                            if ($k == "questions"){
                                $sheet->cell('A1', function ($cell) use ($o, $m) {
                                if ($o == 1) $status = "Conforme"; else $status = "Non Conforme";
                                $cell->setValue($m, $status);
                            });
                            }
                            if ($k == "images"){
                                $sheet->cell('C1', function ($cell) use ($sheet, $i) {
                                    $objDrawing = new PHPExcel_Worksheet_Drawing;
                                    $objDrawing->setPath(public_path('upload/5-20161128152607.png')); //your image path
                                    $objDrawing->setCoordinates('A' . $i);
                                    $objDrawing->setWorksheet($sheet);
                                });
                            }

                        }
                    }
                }
            });
        })->export('xls');
        redirect ('tours/'.$tour);

The 3 foreach are for accessing the right data (same foreach I used in a view and are showing the right data, I even tested those with dd() and shown right data). AFter that, the counter +5 is to give a space for images (as images are appended above a cell and not into it) so that pages (elements) are not shown one above the other.

Any help and advice are welcome !



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2gzTKzy
via IFTTT

Aucun commentaire:

Enregistrer un commentaire