【问题标题】:Method addSelect does not exist方法 addSelect 不存在
【发布时间】:2017-05-08 05:52:18
【问题描述】:

当我尝试在 Laravel 5.2 中执行 addSelect 方法时收到此错误消息

有什么想法吗?

Macroable.php 第 74 行中的 BadMethodCallException: 方法 addSelect 不存在。


这是我的控制器中的功能

公共函数 summaryOfMembersTable() {

    $members = MotherProfile::select('last_name')
        ->orderBy('last_name','ASC')
        ->distinct()
        ->get();

    $count = $members->count(); 

    $mothers = MotherProfile::select(DB::raw('count(*) as user_count, gender'))     
        ->where('gender', '<>', 'F')
        ->groupBy('gender')
        ->get();

    $fullnames = $members
        ->addSelect('first_name')
        ->orderBy('last_name','ASC')
        ->distinct()
        ->get();


    $data = [];     
    $data['members'] = $members;
    $data['memberCount'] = $count;
    $data['mothers'] = $mothers;
    $data['fullnames'] = $fullnames;

    return view( 'user/masterlist/summary-of-members', $data);  

}

我的刀片:

总计为 {{ $memberCount }}

        @foreach ($fullnames as $fullname)
        {{ $fullname }}<br>
        @endforeach
        <hr>
        <div class="page-header" style="text-align:center;">
          <h3 class="pageHeader">
            List of Members
            <br>
          </h3>
        </div>          
        <div class="row">           
            <table class="table table-bordered" style="text-align: left;">
                    <tr>                            
                        <th></th>
                        <th>Full Name (Last, First, Middle)</th>                            
                        <th>KLC</th>
                        <th>Category</th>
                        <th>Mem Type</th>
                        <th>Mem Status</th>                         
                        <th>Date Baptized</th>
                        <th>Mother in Spirit</th>
                        <th>Encoder</th>
                    </tr>
                    <tbody>

                    </tbody>
            </table></div>

【问题讨论】:

  • 任何代码?一个调用 addSelect..?这个 addSelect 在哪里调用?
  • 发送您的代码。
  • 当然,现在将编辑此内容。我认为这是一个常见的 laravel 问题

标签: methods controller laravel-5.2


【解决方案1】:

你得到的是一个集合而不是一个对象。 addSelect 方法属于构建器对象而不是集合。因此,从您的查询中删除 get() 就可以了。

 $members = MotherProfile::select('last_name')
        ->orderBy('last_name','ASC')
        ->distinct()
        ->get();

【讨论】:

  • 您好,感谢您的解决方案。有没有办法让我知道哪些方法属于集合,哪些属于对象构建器?谢谢
  • 好的,我想我能找到。再次感谢@Rodrane,你太棒了!
【解决方案2】:
$members = MotherProfile::select('id','last_name')
        ->orderBy('last_name','ASC')
        ->distinct()
        ->addSelect(DB::raw("'value' as nameFake"))->get();

之前到get()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 2022-06-13
    • 2018-06-16
    • 2018-05-16
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多