Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aishahyaacob's avatar

Property [totalMark] does not exist on this collection instance

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'=> 'QuestionAnswerController@list']) !!}
        <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'));

    }



}

0 likes
11 replies
Tray2's avatar

I'm guessing that you are using the list method in your controller.

Not able to help you with so little information cause I don't know what the heck you are trying to do here.

as step one I would do a ´dd´ on $users to see what it contains.

aishahyaacob's avatar

@TRAY2 - i had create the relationship. this is user.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
//use App\Http\Controllers\Auth;
////use Illuminate\Support\Facades\Auth;

/**
 * @property mixed role
 */
class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'role_id'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function role()
    {
        return $this->belongsTo('App\Role');
    }

    public function isAdmin() {

        if($this->role->name == 'Admin'){

            return true;
        }
        return false;
    }

    public function isStudent() {

        if($this->role->name == 'Student'){

            return true;
        }
        return false;
    }



    public function questionAnswer()
    {
        return $this->hasMany('App\QuestionAnswer');
    }


}

this is QuestionAnswer.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class QuestionAnswer extends Model
{
    
    // to insert data
    protected $fillable = [
        'user_id', 'totalMark',
    ];

    //relationship to user, question,answer
    public function user()
    {
        return $this->belongsTo('App\User');
    }


    public function suggestion()
    {
        return $this->belongsTo('App\Suggestion');
    }

    
    
    
}

can u help me the way how to get the totalMark?

Tray2's avatar

Do a ddon $user->questionAnswer and see what it returns.

I

Snapey's avatar

You cannot do this

<td>{{ $user->questionAnswer->totalMark }}</td>

because questionAnswer is a collection of entries for each user. You need at some point to SUM the values together.

<td>{{ $user->questionAnswer->sum('totalMark') }}</td>
aishahyaacob's avatar

@SNAPEY - it's work. but what i want to do is not to sum up all the mark, i want to display every totalmark for every time the user answer the the questionnaire. for example, if the same user had answered three times, so the list of totalMark will appear three times also. if the first time answered, the totalMatk he got is 4. and for the second totalMark time he answered, he got 3. so i want his name and both of his total Mark is appear . so he can know, for the first time he got 4, and the second time he got 3.

can u give me a help on how to get the totalMark?

Snapey's avatar
Snapey
Best Answer
Level 122

No, I mean like

@foreach ($user->questionAnswer as $qa)
    {{ $qa->totalMark }}
@endforeach 

Please or to participate in this conversation.