0

I have a table that I want to export via excel. I use the method toArray(); and still I get the result as object. Here is my sample code

    $items = \DB::table('users')
            ->join('finances', 'users.id','=','finances.user_id')
            ->join('schoolyears', 'users.school_id','=','schoolyears.school_id')
            ->select('users.name','users.phone','users.section_id', 'users.student_school_id','finances.amount','finances.description','schoolyears.name as syear','finances.date')
            ->where('finances.date', '=' ,(\DB::raw("(select max(`date`) from finances f where finances.user_id=f.user_id)")))
            ->where('users.role','=','4' )
            ->where('users.school_id','=', $sid)
            ->get()->toArray();


        //    dd($items);


        } else {

            return redirect('home')->with('error', 'Invalid access');

        }

        \Excel::create($this->page_title . 's', function ($excel) use ($items) {
            $excel->sheet($this->page_title . 's', function ($sheet) use ($items) {
                $sheet->fromArray($items);
            });

The result I get when i dd($items)

array:2 [▼
  0 => {#558 ▼
    +"name": "Annamarie Morar"
    +"phone": "(0997) 212-7919"
    +"section_id": null
    +"student_school_id": "50"
    +"amount": "500"
    +"description": "New Pays"
    +"syear": "SY-2019-2020"
    +"date": "2019-11-14"
  }
  1 => {#561 ▶}
]

What i want is like this so that i can export it as an excel file

array:9 [▼
  0 => array:10 [▼
    "FirstName" => "Madelynn"
    "LastName" => "Stokes"
    "Gender" => "female"
    "Birthday" => "2013-10-09"
    "Address" => "78A/40 Goodwin Meadow, Poblacion, Iloilo City 1333 Nueva Ecija"
    "PhoneNo" => "+63 (971) 659-8143"
    "Parent" => "Deondre Stokes"
    "SchoolID" => "521"
    "RFID" => "173"
    "Section" => null
  ]
  1 => array:10 [▶]
  2 => array:10 [▶]
  3 => array:10 [▶]
  4 => array:10 [▶]
  5 => array:10 [▶]
  6 => array:10 [▶]
  7 => array:10 [▶]
  8 => array:10 [▶]
]
10
  • 1
    you can try this $array = json_decode(json_encode($object), true); Commented Nov 15, 2019 at 5:55
  • try var_dump intead of dd what result you get? If it only feature dd. Also collect($items)->toArray() should work Commented Nov 15, 2019 at 5:59
  • Thanks @DhavalPurohit . Can you post your answer so that I can mark it as correct? And please explain your answer to me and why the result cant be converted to array when i use toArray();. THANKS Commented Nov 15, 2019 at 6:00
  • @daremachine Alreay answered by dhaval . Please explain to me what is wrong with my code Commented Nov 15, 2019 at 6:01
  • 1
    I mean convert array collection to string and vice versa is ugly and might be memory expensive. Best native solution is @lagbox Commented Nov 15, 2019 at 6:09

1 Answer 1

2

You could convert each object to an array by transforming the Collection then turning the Collection into an array if you had to:

$items = DB::table(...)->.....->get()->transform(function ($item) {
    return (array) $item;
})->toArray();

Laravel 6.x Docs - Collections - Methods - transform

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer sir. I will try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.