How did you defined the relationship between Campus and Carrer ?
Feb 28, 2020
6
Level 1
Select with Vue or Js
Hi guys, regards, i've some doubts i want know if yours can help me please.
First i have two tables:
table_carrers
id name id_campus
1 C1 1
2 C2 1
3 C3 1
4 C4 2
5 C5 2
6 C6 3
table_campus
id name
1 Campus A
2 Campus B
3 Campus C
then i have two select
<label for="campus">Campus</label>
<select id="campus" class="form-control">
<option selected>Choose a campus.</option>
<option>Campus A</option>
<option>Campus B</option>
<option>Campus C</option>
</select>
<label for="carrers">Carrers</label>
<select id="carrers" class="form-control">
<option selected>Choose a carrer</option>
<option>here i want to put the carrer depends on campus</option>
</select>
finally i have on my routes this code
Route::get('auth/register', function(){
//get all the campus
$campuses = Campus::all();
//get all the carrers
$carrers = Carrer::all();
//then i define this realtions on campus model
//but i need seend the key with out load the page
//therefore i get all the data and send
// $carrers = App\Campus::find(2)->carrers;
//then i send twice variables
return view('auth.registerD',['campuses' => $campuses, 'carrers' => $carrers ]);
})->name('registerD');
like you can see above i want know how i can change the second select, i try with axios but dont have request, so then maybe someone can give me some suggestion with Vue or ajax or jquery, please, thanks..
Level 43
One way is to use Vue with this scenario
- Show the first select for campus
- When a campus is selected, show the second select and populate it with the relationship from the selected campus
selectedCampus->carrers
You don't need to add the carrers variable but load the campus with the loaded relationship:
$campuses = Campus::with('carrers')->get();
Please or to participate in this conversation.