0

I need some advice and help on the following topic.

I have a setup of migrations like this.

+--------------------+----------------------+--------------------+
|       plates       |   plates_container   |  container_slots   |
+--------------------+----------------------+--------------------+
| id                 | id                   | id                 |
| plate_container_id | number_of_slots(int) | slot               |
| container_slot_id  |                      | occupied (boolean) |
|                    |                      | plate_container_id |
+--------------------+----------------------+--------------------+
   

The relations between tables goes like this.

Plate

  • Plate belongsTo a platecontainer
  • Plate belongsTo containerslot

PlateContainer

  • platecontainer hasMany containerSlots
  • platecontainer hasMany plates

ContainerSlot

  • ContainerSlot belongsTo plateContainer
  • ContainerSlot hasOne plate

Question

How can I (specified by id) fetch all the number_of_slots and check which of those slots are already occupied and which are not.

So to clarify a bit more. A list like this or similar is what I am asking for.

slots = [

 1 => false,
 2 => false,
 3 => true, 
 4 => false
//etc

];

If you have any other suggestions regarding the database structure, please let me know in the comments. I am slightly doubtful about updating the occupied attribute every time after the user chooses a slot for the plate.

1
  • When you said “how can I (specified by id)…”, which id in particular? Commented Dec 2, 2015 at 16:03

1 Answer 1

1

The Eloquent's collection methods and pluck will help you. Assuming you have defined your models properly for each of the mentioned tables you can do:

PlateContainer::find($id)
              ->container_slots
              ->pluck('occupied', 'id');

This will give you the expected results you are looking for in the following form (which is basically an array):

=> Illuminate\Database\Eloquent\Collection {#1859
     all: [
       1 => true,
       2 => true,
       3 => false,
       4 => true
     ],
   }
Sign up to request clarification or add additional context in comments.

7 Comments

Gold! Exactly what I have been looking for! You have guessed good! I meant id of a platecontainer. Thanks!
I'm glad that it helped you.
I know this is not the original question, but I was just wondering if it's possible to fetch all three attributes (id, slot, occupied) and having those show just like in your above mentioned answer. I check the docs for it, but could find anything similar .
There's a 'only' method where you could pass an array of property name.
I gave this a try. PlateContainer::find(1)->containerSlots->only('occupied', 'slot', 'id'); only its returning empty array. just like this Collection {#467 #items: [] }
|

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.