aishahyaacob
7 months ago

how can i get data from database?

Posted 7 months ago by aishahyaacob

i want to display also totalmarks in suggestion view, my plan is this view display the totalmarks also with the suggestion. here is my display.blade. this is my display view. https://imgur.com/a/yU7wszR

@extends('layouts.app')

@section('content')
    <div class="col-sm-9">
        <h1>Suggestion</h1>

        
        <p>There are some suggestion for you to reduce your stress:</p>

        {{--{!! Form::open(['method' => 'POST', 'action'=> '[email protected]']) !!}--}}


        <div class="container-fluid" id="dynamic_field">
            {{--store data form in array for multiple question--}}

            <table class="table table-bordered" id="dynamic_field">
                <tr>
                    <th>Suggestion</th>
                    <th>Level of Stress</th>

                @if($suggestions)
                    @foreach($suggestions as $suggestion)
                        <tr>
                            <td>{{$suggestion->suggestion}}</td>
                            <td>{{$suggestion->category}}</td>
                        </tr>
                    @endforeach
                @endif

            </table>
        </div>

        <br/>
    </div>

@endsection


here is my controller

<?php

namespace App\Http\Controllers;

use App\Question;
use App\Answer;
use App\QuestionAnswer;
use App\Suggestion;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Route;


class QuestionAnswerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $questions = Question::all();
        $answers = Answer::all();

        return view('student.q_a.index', compact('questions', 'answers'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {


    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $user_id = Auth::user()->id;
        $totalMarks = 0;


        // $key = question number
        // $value = score/scale
        $answer = $request->input('answer');

        foreach ($answer as $key => $value) {
            $totalMarks += $value;
        }

        $questionAnswer = [
            'user_id' => $user_id,
            'totalMark' => $totalMarks / 40 //store in database
        ];

        QuestionAnswer::create($questionAnswer);

        $marks = $questionAnswer['totalMark'];

        if($marks < 1.00){
            return 'No Suggestion';
        }
        else if($marks>=1.00 && $marks <=2.33){
            $suggestions = Suggestion::where('category','Low')->get();
        }
        else if ($marks>=2.34 && $marks <=3.67){
            $suggestions = Suggestion::where('category','Moderate')->get();
        }
        else if ($marks>=3.68 && $marks<=5){
            $suggestions = Suggestion::where('category','High')->get();
        }

//        return $marks;
        return view('student.q_a.display', compact('suggestions','totalMarks'));

    }


    /**
     * Display the specified resource.
     *
     * @param  \App\QuestionAnswer $questionAnswer
     * @return \Illuminate\Http\Response
     */
    public function show(QuestionAnswer $questionAnswer)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\QuestionAnswer $questionAnswer
     * @return \Illuminate\Http\Response
     */
    public function edit(QuestionAnswer $questionAnswer)
    {

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \App\QuestionAnswer $questionAnswer
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, QuestionAnswer $questionAnswer)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\QuestionAnswer $questionAnswer
     * @return \Illuminate\Http\Response
     */
    public function destroy(QuestionAnswer $questionAnswer)
    {
        //
    }


    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function score()
    {
        $user_id = Auth::user()->id;
//      $totalMarks= QuestionAnswer::where('user_id',$user_id);
        $totalMarks= QuestionAnswer::where('user_id',$user_id)->get();
//        $totalMarks = QuestionAnswer::all();
        $timestamps = QuestionAnswer::all();
//

        return view('student.q_a.score', compact('totalMarks', 'timestamps'));
    }



}

Please sign in or create an account to participate in this conversation.