htmlspecialchars() 期望参数 1 是字符串,给定的数组。(laravel)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42383293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
htmlspecialchars() expects parameter 1 to be string, array given. (laravel)
提问by Angelica Rosimo Marcelino
I'm new to Laravel. And i've done researches but i can't seem to find the right answer for my code.
我是 Laravel 的新手。我已经做了研究,但我似乎无法为我的代码找到正确的答案。
So i have this controller
所以我有这个控制器
namespace Modules\Inventory\Http\Controllers;
use Modules\Setup\Init;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Modules\Inventory\Entities\mongodb_logs as mongodb_logs;
use Modules\Template;
class ActivityLogsController extends Controller
{
/**
* Display a listing of the resource.
* @return Response
*/
protected $data;
protected $page_title = 'Login';
function setup($vars = null)
{
$Init = new Init;
$vars['page'] = $this->page_title;
$this->data['template'] = $Init->setup($vars);
return $this->data;
}
public function index()
{
$loglists = mongodb_logs::orderBy('date_added', 'desc')->get();
$this->data['loglist'] = $loglists;
return view('inventory::_logs', $this->setup());
}
}
and my view was just this simple
我的观点就是这么简单
<table id="tbl_logs" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td>Type</td>
<td>User</td>
<td>Date</td>
<td>Message</td>
<td>Before</td>
<td>After</td>
</tr>
</thead>
<tbody>
@if($loglist != null)
@foreach($loglist as $logs)
<tr>
<td>{{$logs->type}}</td>
<td>{{$logs->logged_in_user}}</td>
<td>{{$logs->date_added}}</td>
<td>{{$logs->message}}</td>
<td>{{$logs->before}}</td>
<td>{{$logs->after}}</td>
</tr>
@endforeach
@endif
</tbody>
</table>
I retrieve the loglist from my mongodb collection 'logs'. I successfully retrieved the data you can try it via 'print_r' but when i view it on my view, this is the error that shows
我从我的 mongodb 集合“日志”中检索日志列表。我成功检索了数据,您可以通过“print_r”尝试它,但是当我在视图中查看它时,这是显示的错误
htmlspecialchars() expects parameter 1 to be string, array given
this is the print_r result of my $loglist which i successfulyl retrieved from mongodb print_r result
这是我从 mongodb print_r 结果中成功检索到的 $loglist 的 print_r 结果
here's a few lines of my var_dump.
这是我的 var_dump 的几行。
object(Illuminate\Database\Eloquent\Collection)#408 (1) {
["items":protected]=>
array(40) {
[0]=>
object(Modules\Inventory\Entities\mongodb_logs)#409 (26) {
["connection":protected]=>
string(7) "mongodb"
["collection":protected]=>
string(4) "logs"
["primaryKey":protected]=>
string(3) "_id"
["parentRelation":protected]=>
NULL
["table":protected]=>
NULL
["keyType":protected]=>
string(3) "int"
["incrementing"]=>
bool(true)
["with":protected]=>
array(0) {
}
["perPage":protected]=>
int(15)
["exists"]=>
bool(true)
["wasRecentlyCreated"]=>
bool(false)
["attributes":protected]=>
array(11) {
["_id"]=>
object(MongoDB\BSON\ObjectID)#288 (1) {
["oid"]=>
string(24) "58acfb2b202f941b50002ddf"
}
["user_id"]=>
int(2)
["admin_id"]=>
int(2)
["logged_in_user"]=>
string(5) "admin"
["date_added"]=>
string(19) "2017-02-22 02:44:59"
["type"]=>
string(6) "update"
["message"]=>
string(69) "Deceased Documents ofAniyah Gottlieb ID number 4 successfully updated"
["before"]=>
string(188) " {"id":2,"d_id":4,"death_cert_no":null,"trans_permit_no":null,"crem_permit_no":nu ll,"exhum_permit_no":null,"senior_card_no":null,"disabled_card_no":null,"created _at":null,"updated_at":null}"
["after"]=>
string(211) " {"id":2,"d_id":4,"death_cert_no":"20171542","trans_permit_no":null,"crem_permit_ no":null,"exhum_permit_no":null,"senior_card_no":null,"disabled_card_no":null,"c reated_at":null,"updated_at":"2017-02-22 02:44:59"}"
["updated_at"]=>
object(MongoDB\BSON\UTCDateTime)#287 (1) {
["milliseconds"]=>
string(10) "1672815231"
}
["created_at"]=>
object(MongoDB\BSON\UTCDateTime)#286 (1) {
["milliseconds"]=>
string(10) "1672815231"
}
}
["original":protected]=>
array(11) {
["_id"]=>
object(MongoDB\BSON\ObjectID)#288 (1) {
["oid"]=>
string(24) "58acfb2b202f941b50002ddf"
}
["user_id"]=>
int(2)
["admin_id"]=>
int(2)
["logged_in_user"]=>
string(5) "admin"
["date_added"]=>
string(19) "2017-02-22 02:44:59"
["type"]=>
string(6) "update"
["message"]=>
string(69) "Deceased Documents ofAniyah Gottlieb ID number 4 successfully updated"
["before"]=>
string(188) "{"id":2,"d_id":4,"death_cert_no":null,"trans_permit_no":null,"crem_permit_no":null,"exhum_permit_no":null,"senior_card_no":null,"disabled_card_no":null,"created_at":null,"updated_at":null}"
["after"]=>
string(211) "{"id":2,"d_id":4,"death_cert_no":"20171542","trans_permit_no":null,"crem_permit_no":null,"exhum_permit_no":null,"senior_card_no":null,"disabled_card_no":null,"created_at":null,"updated_at":"2017-02-22 02:44:59"}"
["updated_at"]=>
object(MongoDB\BSON\UTCDateTime)#287 (1) {
["milliseconds"]=>
string(10) "1672815231"
}
["created_at"]=>
object(MongoDB\BSON\UTCDateTime)#286 (1) {
["milliseconds"]=>
string(10) "1672815231"
}
}
回答by Nawin
Your before and after variables are array that's why you getting this error. Try this code:
您的前后变量是数组,这就是您收到此错误的原因。试试这个代码:
<tbody>
@if($loglist != null)
@foreach($loglist as $logs)
<tr>
<td>{{$logs->type}}</td>
<td>{{$logs->logged_in_user}}</td>
<td>{{$logs->date_added}}</td>
<td>{{$logs->message}}</td>
@foreach($logs->before as $before)
<td>{{$before->whatStringYouWant}}</td>
@endforeach
@foreach($logs->after as $after)
<td>{{$after->whatStringYouWant}}</td>
@endforeach
</tr>
@endforeach
@endif
</tbody>
回答by Abdulkareem Mohammed
One or more of the returned object properties is array. try to var_dump($logs)
一个或多个返回的对象属性是数组。尝试var_dump($logs)
I can't comment yet
我还不能评论