0

I have one country model. Which is in one to many relation. One student has many countries. One student can go to many countries.

In Another model named division has no relation to Student. I stored country name in the division and has many data under country with one to many relation with some more model.

I want to get the country name(most cases there is more than one) under one student and with that country names I want to go to division and so on. I might not be able to explain clearly But you will understand with the code.

$countries = DB::table('countries')->where('student_id', '=', $id)->get(); //getting countries
foreach($countries as $country){
   $dis = DB::table('division')->where('country_name', '=', $country->country_name)->get();
}
dd($dis);

I tried

$dis = '';
foreach($countries as $key => $value){
   $dis = DB::table('divisions')->where('country_name', '=', $value->country_name)->get();
}

Where I am getting only one country info. And I tried

$dis = '';
foreach($countries as $key => $value){
  $dis .= $key .':' . $value->country_name . ',';
}

Here I am getting the both country but How to implement this to together! How can I append the data to $dis. I tried many-to-many. But still I am new to laravel so I am stuck after pivot table. I prefer using this more than many-to-many.

3
  • $dis= ''; $concatenated= collect(); foreach($countries as $key => $value){ $dis = DB::table('divisions')->where('country_name', '=', $value->country_name)->get(); $concatenated = $concatenated->concat($dis); } dd($concatenated); If anyone need, It solved with this. Commented Nov 28, 2021 at 6:26
  • it would be better if you used $dis=[] array instead of variable. then you could push to array inside the foreach. then you could get all the countries in the array and it would be more organized Commented Nov 28, 2021 at 6:28
  • Why are you using DB if you have models set up for this? Just to clarify, is it student has many country and country has many division? Latestly, is there a reason you stored the country name in division instead of the country id? Commented Nov 28, 2021 at 8:13

2 Answers 2

1

Hopefully you can achieve your requirement with this way -

  $dis = array();
       foreach($countries as $key => $value){
           $dis[$key] = DB::table('divisions')->where('country_name', '=', $value->country_name)->get();
        }
dd($dis);
Sign up to request clarification or add additional context in comments.

Comments

0

Try array instead of singular variable:

$dis = []; // empty array
foreach($countries as $key => $value){
    // push into array
   $dis[] = DB::table('divisions')->where('country_name', '=', $value->country_name)->get();
}

this would be more cleaner and efficient

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.