The Response content must be a string or object implementing __toString(), quot;booleanquot; given.quot;(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";给定的."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Response 内容必须是一个字符串或实现 __toString() 的对象,"boolean";给定的."
基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01