Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to return a rendered View using Response::json but I'm getting this error:

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

This is my code:

$posts = Post::where( ... )->orderBy( ... )->get();
$data['posts'] = View::make("posts.partials.loadHome")->with("posts", $posts)->render();
$data['msg'] = "ok";

return Response::json($data);

If I var_dump($data) I get this:

<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=2)</i>
  'posts' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'&lt;div class=&quot;post postGrid&quot; data-id=&quot;1864&quot;&gt;&#10; &lt;a target=&#39;_blank&#39; href=&quot;http://objavi.net/posts/1864&quot;&gt;&lt;img src=&quot;http://objavi.net/&quot; id=&quot;imgWrap&quot; data-original=&quot;/thumbs/YAo4wFzIpl76.jpg&quot; class=&quot;lazy&quot; alt=&quot;Deset manje poznatih činjenica o Jozefu Staljinu&quot;&gt;&lt;/a&gt;&#10;  &#10;   &lt;div id=&quot;bodyPreview&quot;&gt;&#10;     &#10;       &lt;a target=&#39;_blank&#39; href=&quot;http://objavi.net/posts/1864&quot;&gt;&lt;h1 class=&quot;previewTitle&quot;&gt;Deset manje poznatih činjenica o Jozefu Staljinu&lt;/h1&gt;&lt;/a&gt;&#10;&#10;     &lt;h3 id=&quot;postInfo&quot;&gt;&#10;                         &lt;a class=&quot;paint&quot; href=&quot;/category/17&quot;&gt;zanimljivosti&lt;/a&gt;&#10; '...</font> <i>(length=12172)</i>
  'msg' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'ok'</font> <i>(length=2)</i>
</pre>

This is posts.partials.loadHome view:

@foreach($posts as $post)

<div class="post postGrid" data-id="{{ $post->id }}">
    <a target='_blank' href="{{ URL::action('PostsController@show', $post->id) }}">{{ HTML::image(null, $post->title, ["id" => "imgWrap", "data-original" => $post->getThumb(), "class" => "lazy"]) }}</a>

    <div id="bodyPreview">

        <a target='_blank' href="{{ URL::action('PostsController@show', $post->id) }}"><h1 class="previewTitle">{{ e($post->title) }}</h1></a>

        <h3 id="postInfo">
            @foreach($post->categories as $c)
                <a class="paint" href="/category/{{ $c->id }}">{{ $c->name }}</a>
            @endforeach
        </h3>

        <h2 class="bodyPreview">{{ strip_tags(truncString($post->body, 160)) }}</h2>

        <div id="createdBy">
            <a href="{{ URL::action('UsersController@show', $post->user()->first()->id) }}">
                {{ HTML::image($post->user()->first()->getAvatar(), $post->user()->first()->username, ["width" => "32", "height" => "32"]) }}

                {{{ $post->user()->first()->username }}}
            </a>
            <label id="timeAgo">{{ localDate($post->created_at); }}</label>
        </div>
    </div>
</div>
@endforeach

I tested this on localhost and everything works fine. What could be the problem?

like image 710
Alen Avatar asked Sep 24 '14 16:09

Alen


5 Answers

Check to make sure there aren't any illegal characters. I had this issue once and ran utf8_encode on the string and that solved the issue.

like image 79
user2778080 Avatar answered Nov 07 '22 18:11

user2778080


I ran in to this blog post and think it gives a pretty good idea for fixing it:

This kind of error will kill you if yo are going to debug it Or trace it step by step, you will never find the solution because this error happens in response, I mean that it will be detected by the framework only after the response is ready to be rendered ,So it is as the message said "the response is boolean". Often it will happen in the view that some variables affect the response content . Just check the view variables one by one ,and remove each of them the try to run again . you will find the variable that cause this error. But before going in this path try to change the view by any other view page (blade-template) and see if the error is still there . if it is not , then the problem in the view page.

like image 32
WillyBurb Avatar answered Nov 07 '22 17:11

WillyBurb


Create the following function

function utf8_encode_deep(&$input) {
    if (is_string($input)) {
        $input = utf8_encode($input);
    } else if (is_array($input)) {
        foreach ($input as &$value) {
            self::utf8_encode_deep($value);
        }

        unset($value);
    } else if (is_object($input)) {
        $vars = array_keys(get_object_vars($input));

        foreach ($vars as $var) {
            self::utf8_encode_deep($input->$var);
        }
    }
}

Try to do the following

utf8_encode_deep($data);
return Response::json($data);
like image 29
Juan Manuel Garcia Carmona Avatar answered Nov 07 '22 19:11

Juan Manuel Garcia Carmona


In my case the error

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

apeared also even when eliminating view variables one by one or when using another view (as suggsested by WillyBurb). So his answer was not working for me.

After a long research I found out that the problem was caused by the following columns:

  • created_at
  • updated_at
  • deleted_at.

After adding them to the $hidden property, the error was gone.

from the docs:

Hiding Attributes From Array Or JSON Conversion

Sometimes you may wish to limit the attributes that are included in your model's array or JSON form, such as passwords. To do so, add a hidden property definition to your model:

class User extends Eloquent {
    //...
    protected $hidden = array(
        'password',
        'remember_token',
        'deleted_at',
        'created_at',
        'updated_at'
    );
    //...
}
like image 3
toesslab Avatar answered Nov 07 '22 19:11

toesslab


You can try this please? im return true or false (a boolean value) and not an Response value like this

return Response::json(array(
        'error' => false,
        'message' => 'Valid Pincode'),
        200
    );
like image 2
Elminson De Oleo Baez Avatar answered Nov 07 '22 18:11

Elminson De Oleo Baez