Latest

Thursday, July 13, 2017

Object of class stdClass could not be converted to string in laravel on excel sheet download

Asked by: Bibin Joy


i was trying to make an excel sheet in laravel by joining 3 tables to make the result of the query to be outputted in an excel sheet

---model to retrieve query result

<?php

namespace App;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
  protected $table= 'product_master';
  protected $primaryKey = 'product_id';

public static function download()
{   
        $data=DB::table('product_master')
    ->join('subcategory_master', 'product_master.subcategory_id', '=', 'subcategory_master.id')
    ->join('category_master', 'product_master.category_id', '=', 'category_master.id')
    ->select('product_master.product_id','product_master.product_name','product_master.part_number','category_master.category_name',
            'subcategory_master.subcategory_name','product_master.net_quandity','gross_weight','product_master.product_type','product_master.description')->where('delete_status','=','0')->get();

    return($data);
}
}

controller--------------

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Product;
use Excel;


class ExcelController extends Controller
{
    public function downloadExcel(Request $request, $type)
    {
        $data= Product::download()->all();



return Excel::create('productmaster', function($excel) use ($data)  {

            $excel->sheet('mySheet', function($sheet) use ($data)

            {


        $sheet->fromarray($data);


            });
        })->download($type);

}}

routue-----

Route::get('downloadExcel/{type}', 'ExcelController@downloadExcel');



Source

No comments:

Post a Comment

Adbox