Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

109
Views
Laravel whereIn get duplicates also

I'm a front-end developer that has been asked to do some Laravel adjustments so please bear with me.

This is the code I'm trying to modify -

$location = Location::where('id', $id)->with('plants')->get();
$plantId = PlantLocation::where('location_id', $id)->get('plant_id');
$plants = Plant::whereIn('id', $plantId)->orderBy('id', 'asc')->get();

The way it works is on the form is that there are 3 dropdowns - Participant, Site(location), Plants. Each dropdown's information is tied to the previous dropdown using JavaScript to populate each one.

The DB has 4 tables - Participants(users), Locations, Plants, Plant Locations. There are relationships between them.

On the form first you select the Participant, the DB is queried and you get a list of Site tied to the Participant. Then you choose a Site and get a list of Plants tied to the Site.

Form Fields

So far everything works well with no issues. Here's what I need to update.

In the Plants Location table say I have 3 unique Plants tied to a Site - Rose, Tulip, Orchid, everything works fine and I see [Rose, Tulip, Orchid] in the Plants dropdown.

But if I have the >1 (greater than 1) of the same plant tied to a site (example Rose, Tulip, Rose), I see [Rose, Tulip]. What I want is [Rose, Tulip, Rose] as my output.

From what I understand, the whereIn statements only shows 1 instance and not duplicates. I have tried every piece of code and solution that I could find on the internet to show the duplicates with no luck. At first I thought the JavaScript was messing with it but I output the ajax request I only see [Rose, Tulip].

Can someone point me in the right direction please? Thank you!

7 months ago ยท Juan Pablo Isaza
Answer question
Find remote jobs