0

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

4
  • can you include the values inside $details and $detailsAns together with your expected output Commented Jan 25, 2018 at 2:47
  • hi @Miggy please check updated question Commented Jan 25, 2018 at 2:53
  • are there any relationships to your questions_table and response_table? if there are none, i think it would be better to add one because an answer data should be connected to a question Commented Jan 25, 2018 at 3:27
  • no there shouldnt be any relationship between the two tables. i just want to view both the data in on page.. Commented Jan 25, 2018 at 5:33

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

7 Comments

but the thing is now that the table dont need to relate to each other, i just want to view both the REPLY and QUERIES in one page. how can i do that? forget about the the ither columns eType, eVal, intent.. Just explain to me how can i view data from 2 tables in one page?
If they don't relate to each other then just use two foreach loops, one for questions and one for answers.
you mean like the first example you did?
I changed the code for just what should be relevant. I'd just build the data structures a little different than you originally did, and loop through them to print in the view. What do you think?
it works in a way but not exactly like what i want.. i mean the code that i did separates the intent but the code you did does not separate the intent it just view everything together
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.