Laravel filtered dropdown with jQuery

Viewed 17

I'm making a laravel project using jQuery. And I'd like to create a multiple-selectable dropdown items based on the value from previous field. To be precise, I'd like to create a self-registration form, where

class AcademicFormation extends Model 
{
protected $fillable = ['name'];
}



class AcademicSubject extends Model 
{
   protected $fillable = ['name', 'is_elective', 'is_optional'];

   public function academic_formations()
    {
        return $this->belongsToMany(AcademicFormation::class);
    }
}



class AcademicRegistration extends Model
{
   protected $fillable = [
        'academic_session_id',
        'academic_formation_id',
   ];

   public function academic_options()
    {
        return $this->belongsToMany(AcademicSubject::class);
    }
}


class AcademicRegistrationController extends Controller
{
    public function create()
    {
        $academic_sessions = AcademicSession::where('published',1)->get()->pluck('name', 'id');
        $academic_formations = AcademicFormation::pluck('name', 'id');
        $academic_options = AcademicSubject::where('is_optional', 1)->get()->pluck('name', 'id');

        return view('app.academicRegistrations.create', compact('academic_sessions', 'academic_formations', 'academic_options');
    }
}

But I don't know how to make a filter inside the create blade, so that when the user select their AcademicFormation, the dropdown of AcademicOption will show only the AcademicSubject which belongs to that specific AcademicFormation.

Can anyone please help, or tell me the keywords/where should I search for tutorials?

Many thanks in advance for any response and help.

1 Answers

You have to send an ajax call upon each selection with the selected key to the backend and it will filter out the related results.

In your case when you select AcademicFormation you will send an ajax call to the backend with the selected AcademicFormation id, the call will then filter out the AcademicSubject having AcademicFormationId equals to the selected AcademicFormationId.

Then you'll have to populate the dropdown using jQuery

Related