【问题标题】:cannot group same value together laravel不能将相同的值组合在一起 laravel
【发布时间】:2018-07-30 09:33:33
【问题描述】:

所以我有一个函数是:

public function checkTS(Request $request, $companyID)
{
  $data = DiraQuestion::where('status', 0)->get();

  return view('AltHr.Chatbot.checkTS', compact('data','companyID'));
}

从这里我输出一个表格:

<table id="myTable" class="table table-striped">
  <thead>
    <tr>
      <th></th>
      <th>Entity Type</th>
      <th>Entity Value</th>
      <th>Synonym</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
  @foreach($data as $b)
    <tr>
      <td><input type="hidden" name="id" value="{{ $b->id }}" readonly></td>
      <td>{{ucwords(strtok($b->eType, '_'))}}</td>
      <td>{{ucwords($b->eVal)}}</td>
      <td>{{ucwords($b->synonym)}}</td>
      <td><a href="{{action('AltHr\Chatbot\ChatbotController@viewTS', [$companyID, $b->id, $b->eType, $b->eVal])}}" class="btn alt-btn alt-btn-black">View</a></td>
    </tr>
  @endforeach
  </tbody>
</table>

目前,它向我显示了如下输出:

我的数据库看起来像:

正如您从第一张图片中看到的那样,我想要做的是将具有相同意图、eType、eVal 的数据库组合在一起,因此与其显示表中的所有值,不如只显示 1 个值,即已经按相同的 3 值组合在一起。我该怎么做?

【问题讨论】:

    标签: php html mysql database laravel


    【解决方案1】:

    您必须在查询中对这些使用 group by。

    public function checkTS(Request $request, $companyID)
     {
       $data = DiraQuestion::where('status', 0)
              ->groupBy('Your-cloumn')->get();
    
       return view('AltHr.Chatbot.checkTS', compact('data','companyID'));
     }
    

    希望对你有帮助

    【讨论】:

    • SQLSTATE[42000]: 语法错误或访问冲突: 1055 'althr.dev.dira_questions.id' 不在 GROUP BY 中(SQL: select * from dira_questions where status = 0分组eVal)
    • 出错后能否定义完整代码
    • @AnilKumarSahu 你是什么意思?
    • SQLSTATE[42000]:语法错误或访问冲突:发生此错误时,您为 groupby() 编写的代码类似;
    • 我使用的代码是基于@Adnan Mumtaz here提供的答案。
    【解决方案2】:

    您可以通过groupBy('cloumn name')

    对相同类型的值进行分组
    public function checkTS(Request $request, $companyID)
     {
       $data = DiraQuestion::where('status','=','0')
              ->groupBy('Your-cloumn')->get();
    
       return view('AltHr.Chatbot.checkTS', compact('data','companyID'));
     }
    

    【讨论】:

    • 我遇到错误:SQLSTATE[42000]: 语法错误或访问冲突:1055 'althr.dev.dira_questions.id' 不在 GROUP BY 中(SQL: select * from dira_questions where @ 987654323@ = 0 分组 eVal)
    【解决方案3】:

    试一试:

      $data = DiraQuestion::where('status', 0)
                ->groupBy('intent', 'eType', eVal')
                ->get();
    

    同时修复'isn't in GROUP BY'的错误:

    在您的config/database.php 中,在mysql 设置中,将'strict' 的值更改为false:

    'strict' =&gt; false,
    这将在查询时删除ONLY_FULL_GROUP_BY

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多