如何使用laravel eloquent查询在一个表中获取一行的两列

时间:2015-01-29 08:59:24

标签: php mysql laravel-4

在Laravel中我可以在这段代码中的一个表中获得两列的值

$customerName = Customer::where('id', '=', Input::get('customerID'))->first()->customer_name;
        $created_at = Customer::where('id', '=', Input::get('customerID'))->first()->created_at;

我想知道如何在一个查询中获取这两个值(customer_name和created_at)

2 个答案:

答案 0 :(得分:1)

我在doc中找到了这种方式:

Customer::where('id', '=', Input::get('customerID'))->lists('customer_name', 'created_at');

http://laravel.com/docs/4.2/queries#selects

答案 1 :(得分:1)

我使用此代码但不确定是否优化

$customerID = Input::get('customerID');
$customer = Customer::findOrFail($customerID);
$customerName = $customer->customer_name;
$created_at = $customer->created_at;