0

Currently running Laravel 4.1.31. In the controller an array is built of users with their id as the key:

$owners = User::get()->lists('username','id');

Printing the owners array out at the controller level would produce the following:

print_r($owners)

// Array
// (
//    [1] => user1
//    [2] => user2
//    [4] => user3  <--- key jumps to 4, this is correct
//    [5] => user4
//    [6] => user5
// )

However once the array is passed to a view the keys are not preserved, printing it out at the view level produces the following:

// Array
// (
//    [1] => user1
//    [2] => user2
//    [3] => user3  <--- key was replaced with 3, this is incorrect
//    [4] => user4       all values from this point on are now shifted
//    [5] => user5
// )

There was no 3 key in the original array however once it was passed to the view everything shifted to fill in the gap. This ends up causing problems as all id's above 2 are now mismatched. How can the arrays keys be preserved?

Full controller method:

public function edit($id) {

  // get the task
  $task = $this->task->find($id);

  // grab all users for owner field
  $owners = User::get()->lists('username','id');

  // grab all projects for project field
  $projects = Project::get()->lists('title','id');

  // add placeholder to beginning of arrays
  array_unshift($owners, 'Select Owner');
  array_unshift($projects, 'Select Project');

  // return show view
  return View::make('tasks.edit', array(
    'task'     => $task,
    'status'   => $this->status,
    'projects' => $projects,
    'owners'   => $owners
  )); 

}
6
  • Can you show us the full controller action please? Commented Jan 13, 2015 at 21:22
  • This is likely why I haven't used lists. Would it work for you if you changed: $owners = User::select("username", "id")->distinct()->get();? Commented Jan 13, 2015 at 21:27
  • @lukasgeiter Added the method. Commented Jan 13, 2015 at 21:40
  • 1
    Do a dd($owners) before the array_unshift and note the results. Then do one after array_unshift, do the keys change then or only when sent to tasks.edit? Commented Jan 13, 2015 at 21:43
  • @TimLewis thats it... array_unshift is not preserving the keys. Commented Jan 13, 2015 at 21:44

2 Answers 2

2

array_unshift changes the keys of your array. It will reset them to sequence of numbers 0,1,2,3,...

Use this to add a value with key "" at the beginning of your array:

$owners = ['' => 'Select Owner'] + $owners;
Sign up to request clarification or add additional context in comments.

Comments

2

As noted in the comments, get rid of array_unshift on the two arrays. In the view, handle that as follows:

<select name="projects">
  <option value="">Select Project</option>
  @foreach($projects AS $project)
  <option value="{{ $project->id }}">{{ $project->title }}</option>
  @endforeach
</select>

Instead of making the first element a generic value, hardcode it before processing the @foreach to make a select.

Hope that helps!

1 Comment

Oh. Well, since I've never once used Laravel's Form::* functions, I didn't know... You make a good point though. I feel like I have more control without using Form::*, so I never bothered with it.

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.