如何在“ - ”

时间:2018-02-05 02:38:59

标签: php html database laravel

嘿所以我的数据库看起来像这样。请只关注第二栏: db

现在我做了这个看起来像: what i have done

所以现在你们都可以看到我做了一个视图,它显示并分离和分组相同的值:

批准-定时时, 审批,违规行为, 取消人, 消除人-1, 抵消人-2

但我现在要做的是将所有“批准”组合在一起并将所有“取消”放在一起,而不是将它们分开,如我的第二个图片所示。我怎样才能做到这一点?我的代码如下:

路线:

Route::get('queries/{companyID}/{entityType}/{entityValue}','Chatbot\ChatbotController@queries');

控制器:

public function queries($companyID, $entityType, $entityValue)
{
  $data = [];

  $details = DiraQuestion::where('company_id', $companyID)->where('eType', $entityType)->where('eVal', $entityValue)->get();

  foreach ($details AS $datum) 
  { 
    if (!isset($data[$datum->intent])) $data[$datum->intent] = ['question' => [], 'answer' => []]; 
    $data[$datum->intent]['question'][$datum->queries] = $datum->id; 
  } 

  $detailsAns = DiraResponses::where('company_id', $companyID)->where('eType', $entityType)->where('eVal', $entityValue)->get();

  foreach ($detailsAns AS $datum) 
  { 
    if (!isset($data[$datum->intent])) $data[$datum->intent] = ['question' => [], 'answer' => []]; 
    $data[$datum->intent]['answer'][$datum->reply] = $datum->id; 
  }

  ksort($data);
  return view('AltHr.Chatbot.queries', compact('data','entityType','entityValue','companyID'));
}

视图:

@foreach($data as $intentName => $questionAnswer)
  <div class="form-group edit-response-container">
    <label data-toggle="collapse" data-target="#collapse-{{$intentName}}" aria-expanded="false" aria-controls="collapse-{{$intentName}}"> {{$intentName}} <i class="fa fa-chevron-down"></i></label>
    <div class="collapse" id="collapse-{{$intentName}}">

    </div>
  </div>
@endforeach

1 个答案:

答案 0 :(得分:0)

我会使用groupBy收集方法并按照以下方式处理第二列:

// change to your model and whatever the second column is named.
$collection = Model::all();
// or DiraQuestion/DiraResponse ..
$collection = ... your query...
// then
$grouped = $collection->groupBy(function ($item, $key) {
    return explode('-', $item['column_2'])[0];
});

<强>更新

// result of DiraResponses query
$grouped = $detailsAns->groupBy(function ($item, $key) {
    return explode('-', $item['column_2'])[0];
});
相关问题