duanmengsuo9302 2018-01-25 02:20
浏览 25
已采纳

如何使用两个表laravel来预取来自同一数据库的数据

I have written a code to view data but I'm having some problems.

From the code, I wrote it's viewing the data from one table which is the $details. But I also wanna view the data from the $detailsAns.

How can I do that? also, can I foreach two data from that two tables?

my code below:

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'));
}

so as you can see I can return the data from foreach for $details but now I want to return data for $detailsAns also. how can I do that?

my database structure:

table1: enter image description here

table2: enter image description here

so to get the data it first has to select the company id, eType, eVal, and intent so from that now i need to display the reply and queries.

The output that i want is as below: enter image description here

So as you can see i can output the questions table in the foreach that i did. and if i change the foreach to $detailsAns then i display the reply.. but i want to view the both now

  • 写回答

1条回答 默认 最新

  • dpdhf02040 2018-01-25 03:28
    关注

    It isn't very clear from the data provided how the tables relate to each other. If the answer has the same id value as the question, it is easy to relate them by structuring the arrays so that they are keyed by the common value.

    $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;
    }
    

    At this point, since you said that there is no relation between the data sets, just pass the data array to the view the way you already are:

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

    And loop through both elements of data, printing out values in the 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题