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

Haider's avatar

Returning a result set of raw query from controller to view

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.

0 likes
26 replies
jlrdw's avatar

return view('employee_home', compact('q1'));

then

foreach ($q1 as $paid_customer) {
    $paid_customer->whatever
    //etc

just convert to blade

Snapey's avatar

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();
Haider's avatar

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

Haider's avatar

@jlrdw the result set of the query has single column. How can I use foreach to output the result?

Haider's avatar

@jlrdw Still the problem persists. it says undefined variable paid_customers

jlrdw's avatar

ok

$q1[0]->paid_customers

But make sure you pass q1

Done quick, of course put in blade correctly

Haider's avatar

@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

jlrdw's avatar

Like @Snapey said you need employee_home.blade.php, And compact like I showed.

Snapey's avatar

change this

return view('employee_home')->with('q1', $q1);

return view('employee_home')->with(compact('q1'));
Haider's avatar

@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.

jlrdw's avatar

Do

return view('employee_home', ['q1'=>$q1]);

Is that a Q or G

Haider's avatar

@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

Haider's avatar

@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

jlrdw's avatar

Show your entire blade file. And what is the exact name?

Haider's avatar

@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>
jlrdw's avatar

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's avatar

Try placing {{$q1[0]->paid_customers}} at top of view file to test

Haider's avatar

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

jlrdw's avatar

In controller try a test like

$myvar = 'hello';

See if that passes.

Haider's avatar

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.