0

I've upgraded my app from Laravel 4.2 to 5.2. This line in the controller grabbed a list of directories:

 $directories = DirectoryModel::lists('name', 'id');

and I sent it compacted to the blade to generate a pull-down menu in the blade:

 return view('citations.createBlank', compact('citation'), compact('directories'));

In the blade, I used the Form collective to generate the pulldown menu as follows:

Form::select('directory_id', [null=>'Please Select'] + $directories, $citation->directory_id, ['id'=>'directory','class'=>'form-control input-sm js-basic-single','required'])

In Laravel 4.2, $directories was an array in the blade. Now in 5.2 it's a Collection (object) and the blade is throwing an error: Object of class Illuminate\Support\Collection could not be converted to int.

How do I convert this to an array in the Controller? Or do I need to rewrite my blade to generate the pulldown differently?

MrMoxy
  • 392
  • 4
  • 13

1 Answers1

1

You need to use ->toArray() method:

$directories = DirectoryModel::lists('name', 'id')->toArray();

By the way:

The lists method on the Collection, query builder and Eloquent query builder objects has been renamed to pluck. The method signature remains the same.

So you better use pluck instead of lists, it's deprecated:

$directories = DirectoryModel::pluck('name', 'id')->toArray();

Upgrade Guide

yrv16
  • 2,205
  • 1
  • 12
  • 22