0

I get my model

$users = User::all();

Pass to my view:

return view('pages.user.index')
        ->with('users', $users);

But now I want to pass into alpine:

<div x-data="{ items: {{ $users }} }">

That all works!

But if I do:

$users = User::paginate(20);

Then:

<div x-data="{ items: {{ $users->items() }} }">

I get:

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

How can I pass a paginated set of data into alpine?

panthro
  • 22,779
  • 66
  • 183
  • 324
  • Does this answer your question? [Laravel - htmlspecialchars() expects parameter 1 to be string, object given](https://stackoverflow.com/questions/43217872/laravel-htmlspecialchars-expects-parameter-1-to-be-string-object-given) – Pejman Kheyri Feb 26 '21 at 17:43

1 Answers1

1
return view('pages.user.index')
        ->with('users', $users->toJson());
<div x-data="{{ $users }}">
  <template x-for="user in data">
    <div x-text="user.name"></div>
  </template>
</div>
keyboardSmasher
  • 2,661
  • 18
  • 20