2015-02-03 7 views
-2

내가 함수의 응답을 반환해야하는 함수를 반환하고 나는 오류개체를 문자열로 변환 할 수 없습니다.

function excel() { 

    // If I don't add return here, then calling excel() will always be empty. 
    // However when calling it I get the error ErrorException 

    return Excel::load($filePath, function($reader) { 
     $results = $reader->get(); 
     return $results; // there's an array here 
    }); 
} 

function test() { 
    return excel(); 
} 

return test(); //Empty when it should have an array. 

message: "Object of class Maatwebsite\Excel\Readers\LaravelExcelReader could not be converted to string" 
type: "ErrorException" 

어떻게 제대로 내가 원하는 것을 얻을 수를 얻을? (결국 $ 결과를 반환하는 것입니다).

편집 : 오류가 있습니다 Laravel에 사용되는 파일에서 발생합니다 :

라인 (406) file: "/App/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Response.php"

그리고 후드 코드가

public function setContent($content) 
{ 
    if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable(array($content, '__toString'))) { 
     throw new \UnexpectedValueException(sprintf('The Response content must be a string or object implementing __toString(), "%s" given.', gettype($content))); 
    } 

    $this->content = (string) $content; 

    return $this; 
} 

없음이다 아이디어가 왜 코드를 통과하는지, 몇 가지 제안이 필요합니다.

+3

그래서이 오류 메시지가 나타나는 줄은 어디입니까? – sectus

+0

Chrome 개발자 도구에서 '네트워크'아래에 있습니다 (이 기능은 POST를 통해 호출됩니다). 내 게시물에 추가. –

+0

"문자열로 변환 할 수 없습니다"는 것은 개체가 문자열을 지원하지 않을 때 문자열과 같이 처리하려고한다는 것을 의미합니다. 'echo $ object'. 그것은'return'과 아무 관련이 없습니다. – deceze

답변

0

그것을 해결하기 위해, 나는로 json_encode이 방법을 사용했다 ". 문자열로 오브젝트를 변환 할 수 없습니다"

return json_encode(Excel::load($filePath, function($reader) { 
    $results = $reader->get(); 
    return $results; // there's an array here 
})); 

을 내 자신의 질문에 대답하는 열쇠였습니다 (잠시 동안 인터넷 검색을해야했습니다).

0
function excel() { 
return Excel::load($filePath)->get();} 
+3

질문은 포괄적 인 대답을받을 가치가 있습니다. –

관련 문제