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

Maison012's avatar

Cant get multiple rows with get method?

I have 2 record with status = Y on db but i can get only first record when i do this query.

   $getStatus = DB::connection('mysql2') ->table('actives') ->where('status', '=', 'Y') ->get();
    
    foreach($getStatus as $key => $status){

        dd($status->status);
        
    }

Should i write any condition here ?

0 likes
28 replies
Sinnbeck's avatar

You are terminating the request with dd. Dump and die

foreach($getStatus as $key => $status){

        dump($status->status);
        
    }
Sinnbeck's avatar

@usertxr I would assume. But as I dont have access to your code and database I cannot be sure.

$getStatus = DB::connection('mysql2') ->table('actives') ->where('status', '=', 'Y') ->get();
dd($getStatus->count()); //how many does it say you have?
Maison012's avatar

@Sinnbeck yes i see. But i am trying to do something more complicated , now i show you

Maison012's avatar

@Sinnbeck when i do this i get an error: id does not exist on this collection instance

 $getStatus = DB::connection('mysql2') ->table('actives') ->where('status', '=', 'Y') ->get();

foreach($getStatus as $key => $status){

    $getTable = DB::connection('mysql2') ->table("custom_" . $status->id) ->skip(0) ->take(10) ->get();
    
  }

i want to add "custom_" before id of each record i will fetch from actives

Sinnbeck's avatar

@usertxr and if you dd status ? What do you get?

foreach($getStatus as $key => $status){
       dd($status);
Sinnbeck's avatar

@usertxr And not a collection.. Are you sure the error comes from that line?

    $getTable = DB::connection('mysql2') ->table("custom_" . $status->id) ->skip(0) ->take(10) ->get();

Any chance you try to get ->ìd` later in the script?

Maison012's avatar

@Sinnbeck but now when i do this function inside foreach loop i get just last id and no both id as i want

      $getTable = DB::connection('mysql2') ->table("custom_" . $status->id) ->skip(0) ->take(10) ->get();
Sinnbeck's avatar

@usertxr well you are overwriting the variable in each loop? I just assumed you used it inside the loop

Maison012's avatar

@Sinnbeck yes i used this variable on this loop

 foreach($getStatus as $key => $status){

$getTable = DB::connection('mysql2') ->table("custom_" . $status->id) ->skip(0) ->take(10) ->get();

 }

and down this i use this loop to insert data what i get on another table

     foreach($getTable as $key => $tabless) {
       
        
        $existing_data_in = DB::table('test')->where("id", $tabless->id)->first();
        if ( ! $existing_data_in) {
            
            DB::connection('mysql')->table('test')->insert([
               
                "user_id"           =>$tabless->id,                    
               ]);
            }
    }
Maison012's avatar

@Sinnbeck like this?

  $getStatus = DB::connection('mysql2') ->table('actives') ->where('status', '=', 'Y') ->get();

foreach($getStatus as $key => $status){

    $getTable = DB::connection('mysql2') ->table("custom_" . $status->id) ->skip(0) ->take(10) ->get();
    //dump($getTable);
    



		foreach($getTable as $key => $tabless) {
       
        
        $existing_data_in = DB::table('test')->where("id", $tabless->id)->first();
        if ( ! $existing_data_in) {
            
            DB::connection('mysql')->table('test')->insert([
               
                "user_id"           =>$tabless->id,                    
               ]);
            }
    }
    }
Maison012's avatar

@Sinnbeck ok i have done this. adn i write a log message in this way

      Log::info( "Success,  " . $tabless->id );

on log files i get just last id

Sinnbeck's avatar

@usertxr again, without knowing where you do so, I cannot help you. My guess is that you do it outside of the loop where you set the variable

Maison012's avatar

@Sinnbeck yes again you are right. But just an other question. Why i am geting only 20 record from this table , where i need to get about 8000 records? i have a paginate(10) to display this record and i can see on page 1 i get 10 record from table 1 and on page 2 i get 10 record by table 2

Sinnbeck's avatar

@usertxr you have 2 in the first table.. And then you limit yourself to 10 on each sub query (take 10)

2*10=20

You only do this once for each row and never change limit or offset

Maison012's avatar

@Sinnbeck i see this. but what shoul i do to get all data and to display with paginate?

(You only do this once for each row and never change limit or offset) and how exactly should i do this?

Maison012's avatar

Forget it. i dont now why idont read my code carefouly. Thanks for your answer . I fix this

anilkumarthakur60's avatar

$activities=DB::table('actives') ->where('status', 'Y') ->get(); when you dd($activities) what do you get

Please or to participate in this conversation.