Guest User

Untitled

a guest
Apr 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. <?php
  2.  
  3. use IlluminateSupportFacadesSchema;
  4. use IlluminateDatabaseSchemaBlueprint;
  5. use IlluminateDatabaseMigrationsMigration;
  6.  
  7. class CreateOrderProductTable extends Migration
  8. {
  9. /**
  10. * Run the migrations.
  11. *
  12. * @return void
  13. */
  14. public function up()
  15. {
  16. Schema::create('order_product', function (Blueprint $table) {
  17. $table->integer('order_id');
  18. $table->integer('product_id');
  19. $table->float('price');
  20. $table->integer('amount');
  21. $table->primary(array('order_id', 'product_id'));
  22. $table->timestamps();
  23. });
  24. }
  25.  
  26. /**
  27. * Reverse the migrations.
  28. *
  29. * @return void
  30. */
  31. public function down()
  32. {
  33. Schema::dropIfExists('orders_products');
  34. }
  35. }
  36.  
  37. <?php
  38.  
  39. namespace App;
  40.  
  41. use IlluminateDatabaseEloquentModel;
  42.  
  43. class Product extends Model
  44. {
  45. protected $fillable = ['name', 'price', 'stock', 'short_description', 'long_description'];
  46.  
  47. public function orders() {
  48. return $this->belongsToMany('Order', 'order_product', 'product_id', 'order_id');
  49. }
  50.  
  51. public function carts() {
  52. return $this->belongsToMany('Cart', 'cart_product', 'product_id', 'cart_id');
  53. }
  54. }
  55.  
  56. public function destroy($id)
  57. {
  58. if ($this->validateID($id)) {
  59. $product = Product::find($id);
  60. //$product->carts()->detach(); --THE PROBLEMATIC LINE
  61. Product::destroy($id);
  62. }
  63. Session::flash('success', $product->name.' has been succesfully deleted.');
  64. return redirect()->to('/products');
  65. }
Add Comment
Please, Sign In to add comment