-1

I created a function in users model to return permission and return as obj. but when i type {{ Auth::user()->permission()->outlineAgreements }} it said "htmlspecialchars() expects parameter 1 to be string, object given". How can i fix it ?

PS: inside test value is an array

"{"outlineAgreements":["view"],"purchaseOrder":["view"],"pwra":["view","create","update","delete"]}"

public function permission()
{
    $permissions = auth()->user()->getAllPermissions()->pluck('name');

    foreach ($permissions as $key => $value) {
        $module          = last(explode(" ", $value));
        $action          = current(explode(" ", $value));
        $result[$module] = $result[$module] ?? [];
        array_push($result[$module], $action);
    }

    return json_decode(json_encode($result));
}
Bryant Tang
  • 251
  • 3
  • 19

1 Answers1

0

Php is complaining about an object print. It expects that the data you are instructing it to print is a string.

Use dd to print out the return of the permissions method for debugging. This way you can see more clearly what data you are about to print out.

{{ dd(Auth::user()->permission()) }}
{{ dd(Auth::user()->permission()->outlineAgreements) }}

If your first box represents that payload data, and you need to access and print all outlineAgreements permissions, and it is an array, you can use implode:

{{ implode(', ', Auth::user()->permission()->outlineAgreements) }}

You can loop through that array too:

@foreach(Auth::user()->permission()->outlineAgreements as $permission)
    {{ $permission }} 
@endforeach

Hope it helped!

Jonathan Martins
  • 734
  • 7
  • 24