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'>=></font> <small>string</small> <font color='#cc0000'>'<div class="post postGrid" data-id="1864"> <a target='_blank' href="http://objavi.net/posts/1864"><img src="http://objavi.net/" id="imgWrap" data-original="/thumbs/YAo4wFzIpl76.jpg" class="lazy" alt="Deset manje poznatih činjenica o Jozefu Staljinu"></a> <div id="bodyPreview"> <a target='_blank' href="http://objavi.net/posts/1864"><h1 class="previewTitle">Deset manje poznatih činjenica o Jozefu Staljinu</h1></a> <h3 id="postInfo"> <a class="paint" href="/category/17">zanimljivosti</a> '...</font> <i>(length=12172)</i>
'msg' <font color='#888a85'>=></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?
That you didn't create
$data = array();
and the local and remote PHP versions differ.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.
first :
this error only happened when you
return false;
it means
Response::json($data) == false
.second :
some character that json can't encode
perhaps,some ASCII char (like
0x00~0x31
that can't display) in your string...so,json encode return false
Although this question is a bit old and your problem probably solved by now, I thought this may be relevant for others. Tl;dr: use
DB::statement("SET NAMES 'UTF8'");
just before retrieving the results from the database.Your data is probably stored in a character set other than UTF-8 in the database, for instance lantin1. As an alternative for encoding the DB results in your application, you might consider letting the DB handle this.
When using MySQL, you can specify the character set to communicate to the server with using
SET NAMES 'charset_name'
. This specifies to the server that queries are sent using this character set, and asks the server to return results using this character set. (see documentation)Laravel expects UTF-8 data. So, in this case you can issue a statement asking to communicate in UTF-8 before selecting results:
If necessary, you can always switch back to another character set afterwards.
In my case the error
apeared also even when eliminating view variables one by one or when using another view (as suggsested by WillyBurb). So hes 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:
I ran in to this blog post and think it gives a pretty good idea for fixing it: