htmlspecialchars()期望参数1是字符串,给定的数组。 (laravel)
我是Laravel新手。我已经做了研究,但我似乎无法为我的代码找到正确的答案。htmlspecialchars()期望参数1是字符串,给定的数组。 (laravel)
所以我有这个控制器
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());
}
}
,我的观点是如此简单的
<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>
我检索我的MongoDB集 '原木' 的loglist。我成功地检索您可以通过“print_r的”,但是当我看到它在我的观点尝试的数据,这是显示
htmlspecialchars() expects parameter 1 to be string, array given
这是我的$ loglist的print_r的结果,我successfulyl从MongoDB中检索到的错误 print_r result
这里是我的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"
}
}
你之前和之后的变量是数组,这就是为什么你得到这个错误。试试这个代码:
<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>
哦!我之前和之后是json_encoded。但即使我从表格视图中删除他们,我仍然得到错误 –
好吧,似乎'前后'是没有问题的。除此之外,我们来看看。什么是$ this-> data ['template'] = $ Init-> setup($ vars)' –
我正在做一个特定的模块,其中我使用了刀片视图模板。通过'$ this-> data ['name_in_view'] = name_in_controller'将控制器的值识别为视图,并且所有'$ this-> data'包含在'$ this-> setup()'中更容易转移价值投注。控制器和视图。 –
一个或多个返回的对象属性是数组。尝试var_dump($logs)
我不能上线回应还
?替换你的if条件@if(!empty($ loglist)) – rahulsm
向我们展示您的打印阵列... – Nawin
Hi @rahul_m,没有指定特定的行。错误只是说 ErrorException在helpers.php行532: htmlspecialchars()期望参数1是字符串,给定的数组(查看:C:\ laragon \ www \模板\模块\库存\资源\视图\ _logs.blade .php) 我试过了你的建议,但如果你删除了那个foreach块,它仍然工作,我仍然得到错误 –