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

mani786's avatar

Call to a member function first() on int

 public function cancel_consolidation_request(Request $request)
    {
        $getData=DB::table('consolidation_requests')
        ->where(array(
            'delete_status'         =>'off',
            'compiling_status'      =>'consolidation',
            'shipment_id'           =>$request->id
        ))
        ->update([
            'shipment_status'     => 'cencel by user'
        ]);

        $firstRec=$getData->first();
        $sumRec=$getData->get()->sum('qty');
        

        DB::table('incoming_packages')->where('id',[$firstRec->package_id])->increment('total_items',$sumRec);

        if (count($sumRec) >0)
        {
        foreach($sumRec as $rec){
            DB::table('incoming_package_infos')->where('id',[$firstRec->package_id])->increment('qty',$rec);
            }
        }

         return redirect('user/consolidating-packages');
    }

When I run this so get this on windows

Call to a member function first() on int

0 likes
4 replies
Sinnbeck's avatar

Yes. Update returns an integer. You need to make a new query

mabdullahsari's avatar

You are assigning the result of the update statement to the $getData variable, which will return the number of affected rows in the database, and then you proceed to call first() on it.

You have to query the data first.

mani786's avatar

@mabdullahsari I am also assigning get() as well in the other variable to get the sum of the records but this is giving the same error for both

Snapey's avatar

@mani786 you have had two good answers. Your update only returns an integer

use a query variable;

 public function cancel_consolidation_request(Request $request)
    {
        $query = DB::table('consolidation_requests')
        ->where(array(
            'delete_status'         =>'off',
            'compiling_status'      =>'consolidation',
            'shipment_id'           =>$request->id
        ));
        
        $query->update([
            'shipment_status'     => 'cancel by user'
        ]);

        $firstRec=$query->first();
        $sumRec=$query->get()->sum('qty');

This is bad code

$firstRec=$query->first();

since you don't order the query, the result of first() will be unpredictable.

Please or to participate in this conversation.