Basaa
Basaa

Reputation: 1685

Returning array in Laravel throws exception (says my array is a boolean)

I'm facing a weird issue in Laravel that I can't figure out. In my controller I fetch several results from a MySQL database. These results are results to a search query that searches in various databases. I shove these results (from the different tables) in an array and return the array.

The error I get is:

The Response content must be a string or object implementing __toString(), "boolean" given.

And is thrown at

vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Response.php#402

Using Barry's DebugBar I've verified that the array is created perfectly fine. All the data I expect is there and I don't see any problems.

Now I would love to show you the array that I return but this sadly is impossible as we're talking about sensitive customer data.

Now this error only sometimes occurs (on certain search words). This would suggest that there is some kind of UTF-8 or quote problem.

How would I start debugging this? What are the possible reasons that Laravel sees my array as a boolean?

Edit: The basic setup of my controller function:

$customers = Customer::where....
$suppliers = Customer::where.....
$products = Customer::where....
$result = array(
    'results' => [
        'customers' => $customers,
        'suppliers' => $suppliers,
        'products' => $products,
    ]
);
return $result;

Upvotes: 1

Views: 455

Answers (2)

tomprouvost
tomprouvost

Reputation: 115

The problem is not about a result from mySQL.

Problem is about the response which contain utf8 chars. To check, try to use json_encodeyour $result, it will return false.

Upvotes: 1

Martijn de Langh
Martijn de Langh

Reputation: 425

If the problem occurs on certain requests, I would var dump the result of the Customer queries and check what kind of result will break the code.

Now there could be an issue with the queries returning null as a result (no object found) When trying to acces the key of that array it returns the value null and a toString will not work.

Upvotes: 0

Related Questions