【问题标题】:how to foreach data from same database with two tables laravel如何使用两个表 laravel 从同一个数据库中获取数据
【发布时间】:2018-07-04 05:11:17
【问题描述】:

我已经编写了一个代码来查看数据,但是我遇到了一些问题。

从代码中,我写它是从一个表中查看数据,即$details。但我也想查看 $detailsAns 中的数据。

我该怎么做?另外,我可以foreach这两个表中的两个数据吗?

我的代码如下:

public function queries($companyID, $entityType, $entityValue)
{
    $data = [];
    $details = DiraQuestion::where('company_id', $companyID)->where('eType', $entityType)->where('eVal', $entityValue)->get();
    $detailsAns = DiraResponses::where('company_id', $companyID)->where('eType', $entityType)->where('eVal', $entityValue)->get();

    foreach($details as $key => $detailsValue)
    {
        if(!array_key_exists($detailsValue->intent, $data))
        {
        $data[$detailsValue->intent] = [];
        }

        if(!array_key_exists($detailsValue->reply, $data[$detailsValue->intent]))
        {
            $data[$detailsValue->intent]['answer'][$detailsValue->reply] = $detailsValue->id;
        }

        if(!array_key_exists($detailsValue->queries, $data[$detailsValue->intent]))
        {
            $data[$detailsValue->intent]['question'][$detailsValue->queries] = $detailsValue->id;
        }
    }

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

如您所见,我可以为$details 返回来自foreach 的数据,但现在我也想返回$detailsAns 的数据。我该怎么做?

我的数据库结构:

表1:

表2:

所以要获取数据,它首先必须选择公司 ID、eType、eVal 和意图,因此现在我需要显示回复和查询。

我想要的输出如下:

如您所见,我可以在我所做的 foreach 中输出问题表。如果我将 foreach 更改为 $detailsAns 然后我会显示回复.. 但我现在想查看两者

【问题讨论】:

  • 您能否将$details$detailsAns 中的值连同您的预期输出一起包含在内
  • 嗨@Miggy,请检查更新的问题
  • 与您的questions_tableresponse_table 有任何关系吗?如果没有,我认为最好添加一个,因为答案数据应该连接到问题
  • 不,这两个表之间不应该有任何关系。我只想查看页面上的两个数据..

标签: php mysql laravel foreach


【解决方案1】:

从提供的数据来看,这些表之间的关系还不是很清楚。如果答案与问题具有相同的id 值,则很容易通过构造数组来关联它们,以便它们以公共值作为键。

$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->id] = $datum->queries;
}

$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->id] = $datum->reply;
}

此时,既然您说数据集之间没有关系,只需按照您的方式将data 数组传递给视图即可:

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

然后循环遍历数据的两个元素,在视图中打印出值:

@foreach ($data['approval-document']['question'] as $id=>$question)
<p>Question #{{ $id }}: {{ $question }}</p>
@endforeach

@foreach ($data['approval-document']['answer'] as $id=>$answer)
<p>Answer #{{ $id }}: {{ $answer }}</p>
@endforeach

【讨论】:

  • 但是现在表格不需要相互关联,我只想在一页中查看回复和查询。我怎样才能做到这一点?忘记其他列 eType、eVal、intent.. 只需向我解释一下如何在一页中查看 2 个表中的数据?
  • 如果它们彼此不相关,则只需使用两个 foreach 循环,一个用于问题,一个用于答案。
  • 你的意思是像你做的第一个例子?
  • 我将代码更改为应该相关的内容。我只是构建与您最初所做的数据结构略有不同的数据结构,然后循环遍历它们以在视图中打印。你怎么看?
  • 它以某种方式工作,但与我想要的不完全一样。我的意思是我所做的代码将意图分开,但您所做的代码并没有将意图分开它只是一起查看所有内容
猜你喜欢
  • 2018-12-22
  • 1970-01-01
  • 1970-01-01
  • 2020-05-08
  • 1970-01-01
  • 1970-01-01
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多