return view('employee_home', compact('q1'));
then
foreach ($q1 as $paid_customer) {
$paid_customer->whatever
//etc
just convert to blade
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am unable to output the result of a raw database query in a view which is as follows
// Controller coding
public function calculate_fees()
{
$q1 = DB::select("Select SUM(fee) as paid_customers from customers where status='Paid'");
return view('employee_home', ['paid_customers'=>$q1]);
}
// View coding
<?
{{$paid_customers}}
An error that says paid_customers is an undefined variable. Please help me fix this problem.
return view('employee_home', compact('q1'));
then
foreach ($q1 as $paid_customer) {
$paid_customer->whatever
//etc
just convert to blade
I assume your employee_home view is a blade file?, i.e. employee_home.blade.php
You also need a get() on the end of your query to actually run the query
$q1 = DB::select("Select SUM(fee) as paid_customers from customers where status='Paid'")->get();
Var_dumping $q1 outputs
array(1) { [0]=> object(stdClass)#243 (1) { ["paid_customers"]=> string(3) "350" } }
Please tell me how to output it in laravel. Because this query's result has a single column. Foreach loops don't seem to work
@jlrdw the result set of the query has single column. How can I use foreach to output the result?
Ok skip the foreach.
@jlrdw Still the problem persists. it says undefined variable paid_customers
Try paid_customers[0]
No effect :-(
ok
$q1[0]->paid_customers
But make sure you pass q1
Done quick, of course put in blade correctly
try {{$paidCustomers}}
@jlrdw I did the following things
// Controller coding
public function fees_paid()
{
$q1 = DB::select("Select SUM(fee) as paid_customers from customers where status='Paid'");
return view('employee_home')->with('q1', $q1);
}
// View coding
{{$q1[0]->paid_customers}}
Still the issue persists. Says Undefined variable: q1
Like @Snapey said you need employee_home.blade.php, And compact like I showed.
change this
return view('employee_home')->with('q1', $q1);
return view('employee_home')->with(compact('q1'));
@Snapey that didn't work For your information, running
echo $q1[0]->paid_customers;
in the controller works and outputs the result correctly. But when I run this in view
{{$q1[0]->paid_customers}}
It says the q1 variable is undefined.
Do
return view('employee_home', ['q1'=>$q1]);
Is that a Q or G
@jlrdw its Q obviously. Q for query. Just for short
return view('employee_home', ['q1'=>$q1]);
does seem to work. :-( Says the same error that q1 is undefined. Looks like the variable q1 is not properly received at the view. Because on controller I echoed the variable. It displayed the result
Try an echo in view.
@jlrdw It didn't work in view. I did it the following way
<?
echo $q1[0]->paid_customers;
Infact this line displayed as it is in HTML view amongst other content
Show your entire blade file. And what is the exact name?
@jlrdw I divided the entire blade file in parts. Because the code was not displayed elsewise. Consider it one file. Some parts didn't view
<td>{{$tmp->id}}</td>
<input type="hidden" name="id" value="{{$tmp->id}}" />
<td>{{$tmp->name}}</td>
<td>{{$tmp->mobile}}</td>
<td>{{$tmp->address}}</td>
<td>{{$tmp->fee}}</td>
<td><select name="payment">
@if($tmp->status == "Paid")
<option value="{{$tmp->status}}">{{$tmp->status}}</option>
<option value="Not paid">Not paid</option>
@else
<option value="Not paid">Not paid</option>
<option value="Paid">Paid</option>
@endif
</select>
</td>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<td><input type="submit" /></td>
</tr>
</form>
@endforeach
@else
@foreach ($C_Data as $tmp)
<form action="{{URL::to('fee_paid')}}" method="POST">
<tr align="center">
<td>{{$tmp->id}}</td>
<input type="hidden" name="id" value="{{$tmp->id}}" />
<td>{{$tmp->name}}</td>
<td>{{$tmp->mobile}}</td>
<td>{{$tmp->address}}</td>
<td><select name="payment">
@if($tmp->status == "Paid")
<option value="Paid">{{$tmp->status}}</option>
<option value="Not paid">Not paid</option>
@else
<option value="Not paid">Not paid</option>
<option value="Paid">Paid</option>
@endif
</select>
</td>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<td><input type="submit" /></td>
</tr>
</form>
@endforeach
@endif
</table>
{{$q1[0]->paid_customers}}
</div>
Ok try in controller to put it in a variable first Like
$pcustomer = $q1[0]->paid_customers;
And pass $pcustomer to view, see if that works. This is weird.
@jlrdw That didn't work either
Try placing {{$q1[0]->paid_customers}} at top of view file to test
The variable is not being passed from controller to view at all. So neither of any solutions to output the variable in the view is working. Have you had a look over my controller coding and view coding? If anything there is hindering the value to be passed to view
In controller try a test like
$myvar = 'hello';
See if that passes.
Neither $myvar is passing and printing there. So it proves that there was no issue with the query's result. Its the problem in the routes or coding. Let me have a look again
Please or to participate in this conversation.