php - 如何使用两个表laravel从同一数据库中获取数据

标签 php mysql laravel foreach

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

在代码中,我编写了查看来自一个表($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'));
}

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

我的数据库结构:

表1: enter image description here

表2: enter image description here

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

我想要的输出如下: enter image description here

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

最佳答案

从提供的数据中还不清楚这些表之间的相互关系。如果答案与问题具有相同的 id 值,则可以通过构造数组轻松将它们关联起来,以便它们由公共(public)值作为键控。

$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 数组传递给 View 即可:

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

并循环遍历数据的两个元素,在 View 中打印出值:

@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

关于php - 如何使用两个表laravel从同一数据库中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48434641/

相关文章:

php - 在 CakePHP 中将斜杠参数传递给 requestAction

javascript - Node.js Sequelize ManyToMany 关系产生不正确的 SQL

laravel - 如何对有序的 "has many"关系进行分页?

javascript - 无法从创建方法内的 Prop 访问对象

javascript - jquery post .fail 即使 php 成功

php - 带有转义字符的 Symfony 控制台输出

php - 单击按钮时如何插入表格?

php - 如何在 PHP OOP 中设置通知消息

php - Zend 框架 sql 查询产生不同的结果

php - Laravel - 如何用 Eloquent 写这个?