aishahyaacob
7 months ago
111
15
Laravel

how to retrieve two data

Posted 7 months ago by aishahyaacob

i want to retrieve two data from database which are name and totalMark and view it in list.blade. here is my list.blade

@extends('layouts.app')

@section('content')
   <div class="col-sm-9">
       <h1 style="text-align:right";>STUDENT ACADEMIC STRESS SCALE (SASS)</h1>

       {{--{!! Form::open(['method' => 'POST', 'action'=> '[email protected]']) !!}--}}
       <table class="table">
           <thead>
           <tr>
               <th>Name</th>
               <th>Total Mark</th>
           </tr>
           </thead>
           <tbody>

           {{[email protected]($)--}}
               {{[email protected]($user_names as $user_name)--}}
                   {{--<tr>--}}
                       {{--<td>{{$user_name->user_name}}</td>--}}
                   {{--</tr>--}}
               {{[email protected]}}
           {{[email protected]}}

           </tbody>
       </table>
       {{--{!! Form::close !!}--}}
   </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 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'));
   }


   public function list()
   {

       $user_name = QuestionAnswer::all();

       return view('admin.studentList.list', compact('user_name', 'totalMarks'));

   }

}

this is the database in questionAnswer https://imgur.com/a/YXHMyQH

how can i retrieve user name through the user id?

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