1

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"
}
2
  • The only difference between the two variations seems to be in the producs array, so why don't you retrieve the category name when retrieving the $quote model. Then you can call the right value within $producs like $products[$quote->category_name]. If you post your controller/model I can help you with that. Commented Mar 17, 2016 at 21:38
  • @JasperVergers Controller and model added as per request.. I'm old school, I don't like eloquent. Many Thanks !! Commented Mar 17, 2016 at 22:05

1 Answer 1

4

You can join category table with category_id. I assume you have table name category with autoincrement column category_id and category_name column for name so you can use that column in select statement.

Model:

   public function get_quote($id){

    return DB::table('quotations')
        ->join('products', 'products.product_id', '=', 'quotations.product_id')
        ->leftJoin('category', 'category.category_id', '=', 'product.category_id')     
        ->select( 'products.name','products.category_id', 'category.category_name','products.price','quotations.product_id','quotations.quantity','quotations.cost','quotations.reference')
        ->where('quotations.reference', '=', $id)
        ->get();
}   

View:

@foreach($quotation as $quote)
                <tr>
                <td>{!! Form::select('product_id[0]',$products[$quote->category_name], $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>
        @endforeach
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help. I have been slow to come back to you on this, as I have been trying to solve it myself without any success. The problem lies with $products[$quote->category_name]. $quote->category_name is coming back as an undefined index. Strangely it is defined within the get_quote query. I have added the print_r() results for both $products and $quotation. Any thoughts you have are very welcome ! Many thanks !

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.