Laravel - 雄辩的物体和阵列夹头

Looking to use eloquent with array chunk but hitting errors that the first parameter needs to be array.

This is how I am doing things:

public function index() {
    $externalAccounts = ExternalUserAccount::all();
    return View::make('user_creator.index', compact('externalAccounts'));
}

Then in my view:

@foreach (array_chunk($externalAccounts, 4) as $key => $externalAccount)
    <div class="form-group">
        {{ Form::label($externalAccount->name, $externalAccount->display_name) }}
        {{ Form::checkbox($externalAccount->name, $externalAccount->id) }}
    </div>
@endforeach

However, if I use to array in my controller:

$externalAccounts = ExternalUserAccount::all()->toArray();

Then I get 'trying to get property of non object etc' error.

How should I do this?

This is untested but looking at the Laravel documentation your best bet would be to use the Laravel Eloquent ORM to do it and remove the filtering in the view

$externalAccounts = ExternalUserAccount::all()->take(4)->get();

Laravel query builder

The following code should work:

$externalAccounts = ExternalUserAccount::all()->toArray();

The ExternalUserAccount::all() call should return a 'Collection' object, which by default implements the ArrayableInterface, which provides the 'toArray()' method.

Make sure:

1) That your ExternalUserAccount class extends the \Eloquent class.

2) That the result of the ExternalUserAccount::all() is an object of type 'Illuminate\Database\Eloquent\Collection'. You can verify that using:

dd(ExternalUserAccount::all());