本文介绍了Response 内容必须是一个字符串或实现 __toString() 的对象,"boolean";给定的."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 Response::json
返回呈现的视图,但出现此错误:
响应内容必须是字符串或实现__toString()的对象,"boolean"给定."
这是我的代码:
$posts = Post::where( ... )->orderBy( ... )->get();$data['posts'] = View::make("posts.partials.loadHome")->with("posts", $posts)->render();$data['msg'] = "ok";返回响应::json($data);
如果我 var_dump($data)
我得到这个:
这是 posts.partials.loadHome
视图:
@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->标题,[id"=>imgWrap",data-original"=>$post->getThumb(),class"=>懒惰"])}}<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", "高度" => "32"]) }}{{{ $post->user()->first()->username }}}</a><label id="timeAgo">{{ localDate($post->created_at);}}</标签></div></div></div>@endforeach
我在 localhost 上进行了测试,一切正常.可能是什么问题?
解决方案 检查以确保没有任何非法字符.我曾经遇到过这个问题并在字符串上运行 utf8_encode 并解决了这个问题.
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?
解决方案 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.
这篇关于Response 内容必须是一个字符串或实现 __toString() 的对象,"boolean";给定的."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!