Hello folks, I’m looking for some help on adding and searching arrays for a project I’m working on.
This is for a training system which I’ve built but now what I need to do is this: when the admin is creating a course, the only instructors which should be returned as selectable are those with the correct qualification. Now they may need to have more than one qualification so I’m thinking of adding a column ‘qualifications’ to the course and instructor and storing the IDs of each qualification so I can later tally them up.
So, for example, my (simplified) tables would look like this:
Qualifications table: [id, name (string)]
ID 1: ‘First Aid’
ID 2: ‘Health and Safety’
ID 3: ‘Food Hygiene’
etc
Instructors table: [id, name (string), qualifications (array)]
ID 1: ‘Fred’ - ‘1’
ID 2: ‘Bob’ - ‘2, 3’
ID 3: ‘Mary’ - 1, 2’
etc
Course: [id, name (string), qualifications (array)]
ID 1: ‘Food hygiene’ - ‘2, 3’ (health and safety, food hygiene qualifications)
ID 2: ‘First aid’ - ‘1, 2’ (first aid, health and safety)
ID 3: ‘Health and Safety’ - ‘2’ (health and safety)
etc
Now, when I’m showing my ‘food hygiene’ course (ID 1), I need to only return Bob since he’s the only instructor with both qualifications.
When I show my ‘Health and Safety’ course (ID 3), it should return both Bob and Mary since they both have the health and safety qualification. Fred doesn’t appear on either of these as he only has a first aid qualification.
The parts I’m not sure about is how to efficiently do the following:
1 I’m adding a qualification to an instructor from a html table where each row displays the qualification name and a button (which will send the ID via ajax to the controller). I know how to send the ajax call, however I don’t understand how I add each qualification ID to the existing array in the controller.
For example, I want Fred to have a Food Hygiene certificate so I need to add ID 3 to his qualification column which currently only has ID 1.
In my controller I’d be doing something like:
$addqualification = Instructor::where(‘id’,1)->update(‘qualifications’, *WHAT DO I USE HERE TO ADD TO AN EXISTING ARRAY?*);
2 I’m now displaying a course. I need to only display the instructors who have the correct qualification(s). In the health and safety course I need to return Bob and Mary as they both have qualification ID 2.
So, my controller would be something like:
// Find the course
$course = Course::find(3);
// Grab the array of qualifications an instructor must have
$qualification_array = $course->qualifications; // this would get the array of course ids which the instructors must have, in this case qualification ID 2 (health and safety)
$instructors = Instructor:: *?*
This is the bit I’m really stuck on!
I need to say:
- Grab the course qualifications array from the courses table and…
- …using the IDs in that array…
- …find all instructors from the instructors table…
- …who have the all matching qualification IDs in their qualifications array
Again, I need to check that each instructor has all qualifications required for the course, not just one (so the food hygiene course would only return Bob since he has both qualifications).
Any help on this would be most appreciated, I've only just got to grips with working with arrays but its this find/comparison part which has got me completely stumped!