where('commission', '>', 1) //->where('id', 1456) ->with('propertyContractUser') ->with('propertyExecutive.executiveType') ->with('propertyContact') ->with('propertyAdditionalInfos.propertyAdditionalInfoKey') ->with('propertyType') ->with('propertyStatus') ->get(); $propertyList = $propertyList ? $propertyList->toArray() : null; $propertyListToExcel = []; foreach ($propertyList as $property) { $numberOfRooms = collect($property['property_additional_infos'])->where('additional_info_key_id',3)->first(); $numberOfRooms = $numberOfRooms['value']; $propertyListToExcel[] = [ 'contract_user' => $property['property_contract_user']['nameSurname'], 'executive_name_surname' => reset($property['property_executive'])['name_surname'], 'name' => $property['name'], 'phone' => $property['property_contact']['view_full_phone'], 'email' => $property['property_contact']['email'], 'executive_position' => reset($property['property_executive'])['executive_type']['name'], 'executive_phone' => reset($property['property_executive'])['view_full_phone'], 'executive_email' => reset($property['property_executive'])['email'], 'web' => $property['property_contact']['web'], 'address' => $property['property_contact']['address'], 'location' => null, //Bölge 'number_of_rooms' => $numberOfRooms, 'category' => $property['property_type']['name'], 'property_pms' => null, 'property_cm' => null, 'commission' => $property['commission'], 'commission_period' => $property['invoice_type'], 'contract_start' => Carbon::createFromTimestamp($property['created_at'])->format('d.m.Y'), 'contract_finish' => null, 'content' => null, 'dns' => null, 'ssl' => null, 'cm' => null, 'third_party' => null, 'golive' => Carbon::createFromTimestamp($property['created_at'])->format('d.m.Y'), 'training' => null, 'panel' => null, 'official_name' => $property['official_name'], 'tax_office' => $property['tax_office'], 'tax_number' => $property['tax_number'], 'tax_address' => $property['property_contact']['address'], 'status' => $property['property_status']['name'], 'id' => $property['id'], ]; } $f = fopen("C:\www\api.extranetwork.com\storage/tmp.csv", "w"); foreach ($propertyListToExcel as $property) { fputcsv($f, $property); } } }