Property [totalMark] does not exist on this collection instance

Posted 3 months ago by aishahyaacob

i want to display name and totalMark, right now able to display name only. i cannot display totalMarl. name is in tableuser while totalMark in table questionAnswer. here is the error https://imgur.com/a/x82Wwsg

this is my list.blade

@extends('layouts.app')

@section('content')
    <div class="col-sm-9">
        <h1 style="text-align:center";>List of student's name</h1>

        {!! Form::open(['method' => 'POST', 'action'=> '[email protected]']) !!}
        <table class="table">
            <thead>
            <tr>
                <th>Name</th>
                <th>Total Mark</th>
            </tr>
            </thead>
            <tbody>
            @foreach ($users as $user)
                <tr>
                    <td>{{ $user->name }}</td>
                    <td>{{ $user->questionAnswer->totalMark }}</td>
                </tr>
            @endforeach
            </tbody>
        </table>
        {{--{!! Form::close !!}--}}
    </div>
@endsection

this is the 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();
        }

       $category=Suggestion::all();
        return view('student.q_a.display', compact('suggestions','category'));

    }


    /**
     * 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)->get();
        $timestamps = QuestionAnswer::all();

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


    public function list()
    {
        $users = User::has('questionAnswer')->with('questionAnswer')->get();
//        $totalMark=QuestionAnswer::all();

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

    }



}

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