how to apply auth?

Posted 2 months ago by aishahyaacob

how can i display the score based on the user id? now, i had problem when user 2 login for the first time, basically it must only show one score. but right now it also display score of user 1. below are my code in score.blade

@extends('layouts.app')

@section('content')


    <div class="col-sm-9">
        <h1 style="text-align:center;">Result</h1>
        {{--<p>Your score (average) of stress level shown below:</p>--}}
        <p>Guideline:</p>
        <p>1.00 - 2.33 (Low)</p>
        <p>2.34 - 3.67 (Moderate)</p>
        <p>3.68 - 5.00 (High)</p>

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

        <div class="container">
        <table class="table table-striped table-bordered">
            <tr>
                <th>Total Mark</th>
                <th>Date</th>
            </tr>
            </thead>

            <tbody>
            {{[email protected](Auth::user()->user_id)--}}

                @if($totalMarks)
                @foreach($totalMarks as $totalMark)
                    <tr>
                        <td>{{$totalMark->totalMark}}</td>
                        <td>{{$totalMark->created_at}}</td>
                    </tr>
                @endforeach
                    @endif
                    {{[email protected]}}

            </tbody>
        </table>
    </div>
    </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::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.