php Laravel - htmlspecialchars() 期望参数 1 是字符串,给定的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43217872/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:47:52  来源:igfitidea点击:

Laravel - htmlspecialchars() expects parameter 1 to be string, object given

phplaravel

提问by Kiddo

I go this error:

我去这个错误:

htmlspecialchars() expects parameter 1 to be string, object given

I'm using in controller:

我在控制器中使用:

$data = '{"pr":{"code":"1"},"ac":[[{"icon":"web","action":"link","url":"asd"}]]}'
$newData = json_decode($data);

And i send it to the view as array: 'data' => $newData And when i try to use $data into the view, it give me that error

我将它作为数组发送到视图:'data' => $newData 当我尝试在视图中使用 $data 时,它给了我那个错误

Tried already to use $data->ac OR $data['ac'] but still the same... Some help, please?

已经尝试使用 $data->ac OR $data['ac'] 但还是一样...请帮忙?

回答by jfadich

When you use a blade echo {{ $data }}it will automatically escape the output. It can only escape strings. In your data $data->acis an array and $datais an object, neither of which can be echoed as is. You need to be more specific of how the data should be outputted. What exactly that looks like entirely depends on what you're trying to accomplish. For example to display the link you would need to do {{ $data->ac[0][0]['url'] }}(not sure why you have two nested arrays but I'm just following your data structure).

当您使用刀片回声时,{{ $data }}它会自动转义输出。它只能转义字符串。在您的数据中$data->ac是一个数组并且$data是一个对象,两者都不能按原样回显。您需要更具体地了解应如何输出数据。究竟是什么样子完全取决于你想要完成什么。例如显示你需要做的链接{{ $data->ac[0][0]['url'] }}(不知道为什么你有两个嵌套数组,但我只是按照你的数据结构)。

@foreach($data->ac['0'] as $link)
    <a href="{{ $link['url'] }}">This is a link</a>
@endforeach

回答by Hernan Huma?a

if your intention is send the full array from the html to the controller, can use this:

如果您的意图是将完整数组从 html 发送到控制器,可以使用以下命令:

from the blade.php:

来自blade.php:

 <input type="hidden" name="quotation" value="{{ json_encode($quotation,TRUE)}}"> 

in controller

在控制器中

    public function Get(Request $req) {

    $quotation = array('quotation' => json_decode($req->quotation));

    //or

    return view('quotation')->with('quotation',json_decode($req->quotation))


}

回答by Ehab Elzeny

You could use serialize

你可以用 serialize

<input type="hidden" name="quotation[]" value="{{serialize($quotation)}}">

But best way in this case use the json_encodemethod in your blade and json_decodein controller.

但在这种情况下,最好的json_encode方法是在您的刀片和json_decode控制器中使用该方法。

回答by Sajjad Aljileezi

This is the proper way to access data in laravel :

这是在 laravel 中访问数据的正确方法:

@foreach($data-> ac as $link) 

   {{$link->url}}

@endforeach

回答by M.Idrish

Laravel - htmlspecialchars() expects parameter 1 to be string, object given.

Laravel - htmlspecialchars() 期望参数 1 是字符串,给定的对象。

thank me latter.........................

以后谢谢我……………………

when you send or get array from contrller or function but try to print as single value or single variable in laravel blade file so it throws an error

当您从控制器或函数发送或获取数组但尝试在 laravel 刀片文件中打印为单个值或单个变量时,它会引发错误

->use any think who convert array into string it work.

-> 使用任何认为将数组转换为字符串的方法。

solution: 1)run the foreach loop and get single single value and print. 2)The implode() function returns a string from the elements of an array. {{ implode($your_variable,',') }}

解决方案:1)运行 foreach 循环并获取单个值并打印。2) implode() 函数从数组的元素返回一个字符串。{{ 内爆($your_variable,',') }}

implode is best way to do it and its 100% work.

内爆是最好的方法,它 100% 工作。