Click here to Skip to main content
16,017,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on a Laravel/Livewire project and there is a table with these Datas:

attribute, value, price

I need to make an array with this data with for loop that return something like this:

<pre lang="PHP">

$property=[
  'attribute1'=>['value'=>'price','value2'=>'price2','...'=>'...'],
  'attribute2'=>['value'=>'price','value2'=>'price2', '...'=>'...'],
  '...'=>['...'=>'...'],

];

for exp

PHP
$property= [
    'size'=>['x'=>'30000','xl'=>'35000'],
    'material'=>['wood'=>'55000','glass'=>'85000']
];



What I have tried:

I try this but it returns wrong data



<pre lang="PHP">
public $product,$key,$properties;

public function mount($id){
    $this->product=Product::with('attr')->findOrFail($id);
  
    $db=DB::table('attr_product')->where('product_id',$id)->get()->unique('attr_id')->count();


    for ($I=0; $I<$dB;$I++) {
        $attr_id=$this->product->attr[$i]->id;
        $attr=$this->product->attr[$i]->title;
        $value=$this->product->attr[$i]->pivot->value;
        $price=$this->product->attr[$i]->pivot->price;
        $pivotTableCount=DB::table('attr_product')->where('attr_id',$attr_id)->count();
        for ($j=0;$j<$pivotTableCount;$j++){
           $vp=array($value=>$price);
        }


        $this->properties[]=[$attr=>$vp];

    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900