【问题标题】:How to Assign Column Names to the Laravel paginate() Method如何为 Laravel 的 paginate() 方法分配列名
【发布时间】:2015-03-13 19:18:33
【问题描述】:

我的联系人模型中有这个:

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' 语句分配给我想要的列,这意味着我得到的是 'Column first_name is ambiguous.

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

干杯

【问题讨论】:

    标签: laravel laravel-4 pagination


    【解决方案1】:

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

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

    Specifying A Select Clause下所述

    【讨论】:

      猜你喜欢
      • 2018-03-29
      • 2018-12-28
      • 2020-04-12
      • 1970-01-01
      • 2019-05-16
      • 2014-11-08
      • 1970-01-01
      • 2013-08-02
      相关资源
      最近更新 更多