Accessing specific column using Eloquent & Laravel
<p>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.</p>

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

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

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

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

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

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

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

<p>Using Eloquent, I could do something like <code>Form::select('size', array('L' =&gt; 'Large', 'S' =&gt; 'Small'))</code>, 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 <code>Form</code> statement to use <code>$users[0]-&gt;display_name</code> for the first entry, <code>$users[1]-&gt;display_name</code> for the second, etc... ?</p>