Htmlspecialchars() expects parameter 1 to be string array given

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

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

asked Apr 4, 2017 at 21:18

4

When you use a blade echo {{ $data }} it will automatically escape the output. It can only escape strings. In your data $data->ac is an array and $data is 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).

@foreach($data->ac['0'] as $link)
    This is a link
@endforeach

answered Apr 4, 2017 at 21:35

Htmlspecialchars() expects parameter 1 to be string array given

jfadichjfadich

5,8802 gold badges19 silver badges30 bronze badges

2

if you real intention is to send the full array from the html to the controller, you can use the following code:

from the blade.php:

  

in controller

    public function Get(Request $req) {

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

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


}

answered Apr 19, 2018 at 18:16

You could use serialize


But best way in this case use the json_encode method in your blade and json_decode in controller.

Htmlspecialchars() expects parameter 1 to be string array given

brass monkey

4,92310 gold badges33 silver badges52 bronze badges

answered Aug 13, 2018 at 14:56

Ehab ElzenyEhab Elzeny

2044 silver badges6 bronze badges

0

This is the proper way to access data in laravel :

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

   {{$link->url}}

@endforeach

Htmlspecialchars() expects parameter 1 to be string array given

answered Apr 2, 2019 at 6:17

Htmlspecialchars() expects parameter 1 to be string array given

Try use in your collection the json_encode().

https://www.php.net/manual/pt_BR/function.json-encode.php

public static function getTeamMemberInto($user_id){
    
    $team = DB::table('members_team')
                ->join('teams', 'teams.id', '=', 'members_team.teams_id')
                ->join('employees', 'employees.id', '=', 'teams.leader_employees_id')
                ->where('members_team.employees_id', '=', $user_id)
                ->select('*')->first();
    // dd($team);
    if($team){
        return json_encode($team, true);
    }else{
        return false;
    }
                        
}

Htmlspecialchars() expects parameter 1 to be string array given

Sven Eberth

2,97312 gold badges22 silver badges27 bronze badges

answered Jun 25, 2021 at 17:22

Htmlspecialchars() expects parameter 1 to be string array given