Within the my Quotation module, Products can be grouped under a variety of different categories. Categories can also be created and deleted. The code below works fine but only because I have hardcoded the categories. You will see that if category_id = 1, then show products belonging to "cars". If it is "2", then show "motorcycles".
This is completely inflexible. I want to be able to dynamically create a new category, and have that new category presented on my form. So what can I do, if anything, to allow the dynamic creation or deletion of a category ?
I am using Laravel, however, a PHP answer is just as good.
The code is a Laravel View but I think it is pretty obvious what is going on:
@foreach($quotation as $quote)
@if($quote->category_id == 1)
<tr>
<td>{!! Form::select('product_id[0]',$products['cars'], $quote->name, ['class'=>'product_id']) !!}</td>
<td>{!!Form::text('quantity',$quote->quantity) !!}</td>
<td id="price">{{$quote->price}}</td>
<td id="cost">{{$quote->cost}}</td>
</tr>
@endif
@if($quote->category_id == 4)
<tr>
<td>{!! Form::select('product_id[0]',$products['motorcycle'], $quote->name, ['class'=>'product_id']) !!}</td>
<td>{!!Form::text('quantity',$quote->quantity) !!}</td>
<td id="price">{{$quote->price}}</td>
<td id="cost">{{$quote->cost}}</td>
</tr>
@endif
@endforeach
Controller:
public function quote_edit($id)
{
$customer = $this->quotation->get_customer($id); //array
$products = $this->quotation->get_products(); //array of collections
$quotation = $this->quotation->get_quote($id); //array
$count = count($quotation);
return View('quotations/edit_quote_test2', compact('count','quotation', 'products', 'customer') ) ;
}
Model:
public function get_quote($id){
return DB::table('quotations')
->join('products', 'products.product_id', '=', 'quotations.product_id')
->select( 'products.name','products.category_id','products.price','quotations.product_id','quotations.quantity','quotations.cost','quotations.reference')
->where('quotations.reference', '=', $id)
->get();
}
Here is a sample of the print_r() results for $products
{id: "7",
name: "Stanley Hook Blades 11-939",
product_id: "3463",
description: null,
long_description: null,
price: "62.00",
category_id: "7",
color: null,
coverage: null,
barcode: null,
qrcode: null,
box_quantity: null,
supplier_id: "1",
location_id: null,
alternate_id: null,
created_at: "0000-00-00 00:00:00",
updated_at: "0000-00-00 00:00:00",
category_name: "tools"
}
Here is a sample of the print_r() results for Quotation:
{
name: "asphalt",
category_id: "9",
category_name: "tear_install",
price: "300.00",
product_id: "3453",
quantity: "1",
cost: "300.00",
reference: "jac310316-75",
total: "15900.00"
}
$quotemodel. Then you can call the right value within$producslike$products[$quote->category_name]. If you post your controller/model I can help you with that.