More laravel help :(

Derek Smalls
Derek Smalls
Eat shit!NewcastlePosts: 2,508 edited March 5 in Web Development
I am fairly new to laravel and I'm trying to build an 'add to basket' script. It consists of 3 tables.

- basket - id, session_id
- basket_product - id, basket_id, product_id
- basket_options - id, basket_id, product_id, basket_product_id, opt_val, opt_name

Basically I am trying to insert to all 3 tables when adding a product to basket:

- Firstly it inserts the current session id to 'basket'
- Then inserts the product_id & basket_id to basket_product
- Then inserts the options & IDs to basket_options where basket_product_id = basket_products.id

The reason for the basket_products having an id and using it in the basket_options table is because the same product_id may be in there several times if someone wants to buy the same item but with different options attached

I have tried using basket_product as a pivot table and doing
[php]
$Basket = Basket::find($basket_id);
$Basket->options()->attach($input['product_id']);
[/php] (what happened to code tags?)

But to then run the next part (inserting options) I need to have the id of the pivot table

My question is a) how would I get that ID b) is there a better way of doing this all round?

Comments

  • calder12
    calder12
    Senior Member Posts: 13,496 edited March 5
    PHP tags are no more, you have to just use actual code tags.

    Is the accepted answer here what you're looking for? http://stackoverflow.com/questions/21084833/laravel-get-last-insert-id-using-eloquent
  • Derek Smalls
    Derek Smalls
    Eat shit! NewcastlePosts: 2,508
    Unfortunately no. I found that worked ok for normal inserts but not with attach on a pivot table.
  • Derek Smalls
    Derek Smalls
    Eat shit! NewcastlePosts: 2,508
    Thanks. I'll give that a whirl in the morning. I was kind of hoping it could all be done in a single query. I think this way it is one query to insert the basket, then another to retrieve the pivot id, then another to insert the options. Unless you know a better method?
  • calder12
    calder12
    Senior Member Posts: 13,496
    Nope sorry, I never used the pivot tables so I'm just shooting in the dark based on your info. If Joe were around he could probably help, but he only pops in once in a blue moon.
  • calder12
    calder12
    Senior Member Posts: 13,496
    Nope sorry, I never used the pivot tables so I'm just shooting in the dark based on your info. If Joe were around he could probably help, but he only pops in once in a blue moon.
Sign In or Register to comment.
© Copyright 2003 - 2016 - DT by Kooc Media