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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?