Support the ongoing development of Laravel.io →
Database Eloquent Views

Hi fellow laravel developers! I am a newbie here and is having a hard time looking for the answers to my questions. Hopefully, I can find answers here in laravel.io.

My problem is querying two tables in a function, I did not receive any errors of my syntax however there are no results echoed in my User Interface, I supposed it is because I only declared one variable for two queries which resulted to overwrite the first query. I tried to do a nested array but I can't seem to get the correct syntax. It would be much appreciated if someone can help me with this minor problem.

This is my Eloquent Query:

public function displayAssessment()
	{
  		$results = DB::table('grades')
            ->join('subjectblocking', 'grades.blockcode', '=', 'subjectblocking.blockcode')
            ->join('subjects', 'subjectblocking.subjectcode', '=', 'subjects.subjectcode')
        	->select('grades.studentid','subjectblocking.subjectcode','subjects.price')
        	
            ->orWhere(function($query)
            {
                $query->where('grades.studentid', '=', '2013-F0218')
   	        			->where('sem', '=', '1')
                    	->where('sy', '=', '2013-2014');
            })
            ->get();

            $results = DB::table('subjects')
            ->join('subjectblocking', 'subjects.subjectcode', '=', 'subjectblocking.subjectcode')
            ->join('grades', 'subjectblocking.blockcode', '=', 'grades.blockcode')
        	->select('subjects.numofunit as total_units','subjects.price as total_tuition')
        	
            ->orWhere(function($query)
            {
                $query->where('grades.studentid', '=', '2013-F0218')
   	        			->where('sem', '=', '1')
                    	->where('sy', '=', '2013-2014');
            })
            ->sum('subjects.numofunit','subjects.price');
  		
	
	return View::make('users.assessment')->with('results', $results);
}	

The first query is supposed to list all subjects with its price on a specific student, The second query is supposed to output the sum of the prices of a subject and the sum of the num of units on a specific student.

This is how I call it on my views:

<div id="list" class="well">
            <table class="table">
                <thead>
                    <th>Student ID</th>
                    <th>SubjectCode</th>
                    <th>Price</th>
                    <th>Total Units</th>
                    <th>Total Tuition</th>
                 
                </thead>

                @if (is_array($results))
						{
						    @foreach ($results as $result)
						    {
						        <tr>
                    
                    <td>{{$result->studentid}}</td>
                    <td>{{$result->subjectcode}}</td>
                    <td>{{$result->price}}</td>
                     <td>{{$result->total_units}}</td>
                    <td>{{$result->total_tuition}}</td>
                 
                </tr>		 
						    }
						    @endforeach
						}
                
               @endif
            </table>
        </div>
@stop
Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.