使用Eloquent&Laravel访问特定列

I'm teaching myself Laravel by building an application that, amongst other things, fetches data from default Wordpress tables, and I've run into a best-practices question that I'm not sure how to tackle.

I've got a very simple model for the User database that looks like this:

namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
    public $table='wp_users';
    protected $primaryKey='user_id';
}

And I'm able to fetch it into an array using a line like $users = User::all(); in my controller that I can then send into my view in any number of different ways.

Now; my question for best practices has to do with accessing multiple values from a particular column or from a set of columns from the database on a website.

Say I want to create a dropdown list that contains the display_name from the database as the ... well, displayed text, and the user_login as the option value that gets passed in the form.

In the "good old" days, I could do this with something like a foreach statement that pushes <?php echo $whatever; ?> into the relevant places in the html, but it's a very ugly solution. I mean ... just look at this stuff:

<?php foreach ($users as $key => $value) {
    echo "<option value='" . $key . "'>" . $value['name'] . "</option>";
} ?>

Using Eloquent, I could do something like Form::select('size', array('L' => 'Large', 'S' => 'Small')), and I'm sure there's a way of passing an array into this statement, but that's exactly my problem ... I just can't seem to figure out how to do it. How do I tell the Form statement to use $users[0]->display_name for the first entry, $users[1]->display_name for the second, etc... ?

in your controller do

$users = User::get()->pluck('name','user_id');

in your form

Form::select('size', $users)

This will simply build your dropdown list for you.

Assuming you passed the data to the view and use Blade as the templating engine:

<form method="POST" action="" id="form">
    @foreach($formFields as $key => $val)
        <input name="{{ $key  }}" value=" {{ $val }}">
    @endforeach
</form>

You can use the old Laravel Form package by installing it from the Collective - https://laravelcollective.com/

Once you have that installed you can use the following to generate an array that will use the user_id as the value for each select option and display_name as the options text:

$users = User::where('something', 'something')->lists('display_name', 'user_id');

Then in your blade template:

{!! Form::select('users', $users) !!}