-1

I created a function in users model to return permission and return as obj, but when I type:

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

it says:

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));
}
4
  • What exactly are you trying to output? Commented Sep 15, 2020 at 3:19
  • i wanna type "Auth::user()->permission()->outlineAgreements" can be output and array which is ['view', 'update', delete] Commented Sep 15, 2020 at 3:32
  • is this your error .? stackoverflow.com/questions/43217872/… Commented Sep 15, 2020 at 3:38
  • I am not familiar with the function called last(). Commented Dec 3, 2024 at 21:28

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.