You are terminating the request with dd. Dump and die
foreach($getStatus as $key => $status){
dump($status->status);
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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 ?
You are terminating the request with dd. Dump and die
foreach($getStatus as $key => $status){
dump($status->status);
}
@Sinnbeck I see.
@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?
@Sinnbeck i get 2.
I replace dd with dump and i get both of my record
@usertxr Then your code works as expected and you are getting both rows :)
@Sinnbeck yes i see. But i am trying to do something more complicated , now i show you
@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
@usertxr and if you dd status ? What do you get?
foreach($getStatus as $key => $status){
dd($status);
@Sinnbeck if i do this i get just first record
@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?
@Sinnbeck yes this is the problem . I fixed thanks for that
@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();
@usertxr well you are overwriting the variable in each loop? I just assumed you used it inside the loop
@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,
]);
}
}
@usertxr you would need to do this inside the first foreach
@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,
]);
}
}
}
@usertxr exactly. Otherwise you only run it on the last instance
@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
@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
@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
@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
@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?
@usertxr can I suggest you watch this? https://laracasts.com/series/php-for-beginners
Should give you a better understanding of the basics of php. It seems you might have a few things you need to understand better regarding loops and variables :)
@Sinnbeck thanks for that. But for now can you show me how to fix this?
@usertxr don't copy data and show data in the same code. Create a new route that gets the data you need and use pagination https://laravel.com/docs/8.x/pagination
Forget it. i dont now why idont read my code carefouly. Thanks for your answer . I fix this
@usertxr hope I didn't get you down. It can be complex to work out sometimes.
$activities=DB::table('actives') ->where('status', 'Y') ->get();
when you dd($activities) what do you get
Please or to participate in this conversation.