laravel - 如何为 Laravel paginate() 方法分配列名称

标签 laravel laravel-4 pagination

我的联系人模型中有此:

public static function contacts($perPage = 10)
{
    $order_type = '.desc';
    $order = Session::get('contact.order', 'first_name' . $order_type); // Set the default order to created_at DESC
    $order = explode('.', $order);

    $columns = array(
        'contacts.id as id',
        'contacts.first_name as first_name',
        'contacts.last_name as last_name',
        'contacts.telephone_number as telephone_number',
        'contacts.email as email',
        'accounts.company_name as account',
        'users.first_name as am_first_name',
        'users.last_name as am_last_name'
    );

    $contacts = DB::table('contacts')
        ->leftJoin('accounts', 'account_id', '=', 'accounts.id')
        ->leftJoin('account_managers', 'accounts.account_manager_id', '=', 'account_managers.id')
        ->leftJoin('users', 'user_id', '=', 'users.id')
        ->orderBy($order[0], $order[1])
        ->paginate($perPage, $columns);

    return $contacts;
}

它没有将我的“as”语句分配给我想要的列,这意味着我得到“列first_name 不明确”。

不能用 Paginate 方法来做到这一点吗?

干杯

最佳答案

您需要使用select($columns)...

$contacts = DB::table('contacts')
    ->select($columns)
    // other bits
    ->paginate($perPage);

Specifying A Select Clause 中所述

关于laravel - 如何为 Laravel paginate() 方法分配列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16460062/

相关文章:

php - Laravel 使用 Eloquent 插入数据有两个有很多关系

php - 有没有办法使用 Eloquent 来做出这样的陈述?

javascript - jQuery 表格分页 PHP

Spring + Thymeleaf - 如何实现列表的分页

php - 将 [title] 添加到可填充属性以允许对 [App\Post] 进行批量分配

php - 为不同的查询克隆 Eloquent 模型

php - 我怎样才能在 Laravel 5.3 中得到 guzzle 6 的响应?

mysql - 在 CakePHP 1.2 中显示 Oracle 分页查询

php - Twilio 不显示交付状态

mysql - FIND_IN_SET 查询无法在 laravel 5.7 中工作