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

gixroq's avatar

Query builder returning null

I am trying to run the query below but I keep getting null return.

$result = Order::with(['customers', 'team'])->where('customers_id','=', $custid)->select(sprintf('%s.*', (new Order)->table));

Then I changed it to

$result = Order::with(['customers', 'team'])->where(function($query) use ($cust) {$query->where('customers_id', $cust);})->select(sprintf('%s.*', (new Order)->table));

still returns null.

customers_id belongs to the orders table and references the customers table. I am getting the $custid from the url parameter.

If I replace $custid with an integer for example 3, it returns all orders for the customer with id 3. I have spent hours on trying to resolve the issue. I have hosted on heroku and localhost but nothing works.

I am currently using Windwos 8.

0 likes
7 replies
gixroq's avatar

This is a url parameter

I have this in a blade file a button href="{{ route('admin.orders.index', ['cid' => $row->id]) }}

then in my orderscontroller@index

$cust = $request->cid;

Then this is the output of the querylog

select count(*) as aggregate from (select '1' as row_count from orders where (customers_id is null) and orders.deleted_at is null) count_row_table

select orders.* from orders where (customers_id is null) and orders.deleted_at is null

UsmanBasharmal's avatar

Your problem is the way you're getting the url parameter. Put your url parameter in your method declaration.

Check the Laravel docs under Dependency Injection & Route Parameters here

public function index(Request $request, $custid) {

if it worked, mark it as best answer thank you

gixroq's avatar

I have done that and I am getting the $custid.

Now my datatable is coming up with an error

DataTables warning: table id=DataTables_Table_0 - Ajax error

mkshingrakhiya's avatar

Either remove the select part entirely from the statement or specify it like sprintf('%s.*', with(new Order)->getTable()).

gixroq's avatar

Hi there thanks for your help. Though I was trying to use the datatables, unfortunately it did not work. I have switched to using normal html tables and this works.

Thanks once again for your help.

mkshingrakhiya's avatar

Hey there, could you please mark it as a "Best Answer". It helps everyone finding similar solutions and will also help me.

Please or to participate in this conversation.