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

MCLTano's avatar

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..

0 likes
6 replies
Amaury's avatar

How did you defined the relationship between Campus and Carrer ?

MCLTano's avatar

Campus:

 public function carrers(){
        return $this->hasMany('App\Carrer');
    }

Carrer

public function campus(){
        return $this->belongsTo('App\Campus');
    }
Amaury's avatar
Amaury
Best Answer
Level 43

One way is to use Vue with this scenario

  1. Show the first select for campus
  2. 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();
Amaury's avatar

You're welcome. Don't forget to mark the conversation as closed if it is. Thanks!

MCLTano's avatar

so, may be you have some tutorial or article where i can found some example of get data of routes with vue or js, regards

Please or to participate in this conversation.