Home > OS >  How do i fetch and show multiple data with multiple query which is dynamically generated
How do i fetch and show multiple data with multiple query which is dynamically generated

Time:08-25

I have inserted multiple machine ids( separated with , ) in a single cell which I got from a multi select dropdown, now i have all the machine ids but i want to fetch all the data related to those machines( stored in table name 'machines' ) and show the data in the same page for all the ids.

Hear is the image of my table

public function ShowProducts(Request $Request){
        $ProductTitle = $Request->id;
        $MillDetails = DB::table('plants')->where('PlantName',"=",$ProductTitle)->first();
        $Plants = DB::table('plants')->where('status',"=",'Active')->get();
        $AvailableMachines = $MillDetails->Machines;
        return $riun = explode(',',$AvailableMachines);
      //  return view('Site.mill',compact('MillDetails','Plants'));
    }

now I need help with fetching and showing the all the machines and related data to it.

well I am new to Laravel so any help will be really appreciated

CodePudding user response:

Try this

$MillDetails = DB::table('plants')->where('PlantName',"=",$ProductTitle)->first();

Extract machine ids from MillDetails and you can directly use in Machine model.

$machines = Machine::whereIn('id',explode(',',$MillDetails->machine_ids))->get(); // use Model and column name as per yours

CodePudding user response:

You can make function like this way. it will give you your disered output. add this code in your plant model file Plant.php

public function getMachinesAttribute()
{
    $machines= explode(',',$this->machines);
    return Machine::find($machines); //here Machine is your App\Models\Machine
}

you can now use it like this way

 $MillDetails = Plant::where('PlantName',"=",$ProductTitle)->first(); //here Plant is your App\Models\Plant
 $MillDetails->Machines;

may this will help you ...

  • Related