Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use IlluminateSupportFacadesSchema;
- use IlluminateDatabaseSchemaBlueprint;
- use IlluminateDatabaseMigrationsMigration;
- class CreateOrderProductTable extends Migration
- {
- /**
- * Run the migrations.
- *
- * @return void
- */
- public function up()
- {
- Schema::create('order_product', function (Blueprint $table) {
- $table->integer('order_id');
- $table->integer('product_id');
- $table->float('price');
- $table->integer('amount');
- $table->primary(array('order_id', 'product_id'));
- $table->timestamps();
- });
- }
- /**
- * Reverse the migrations.
- *
- * @return void
- */
- public function down()
- {
- Schema::dropIfExists('orders_products');
- }
- }
- <?php
- namespace App;
- use IlluminateDatabaseEloquentModel;
- class Product extends Model
- {
- protected $fillable = ['name', 'price', 'stock', 'short_description', 'long_description'];
- public function orders() {
- return $this->belongsToMany('Order', 'order_product', 'product_id', 'order_id');
- }
- public function carts() {
- return $this->belongsToMany('Cart', 'cart_product', 'product_id', 'cart_id');
- }
- }
- public function destroy($id)
- {
- if ($this->validateID($id)) {
- $product = Product::find($id);
- //$product->carts()->detach(); --THE PROBLEMATIC LINE
- Product::destroy($id);
- }
- Session::flash('success', $product->name.' has been succesfully deleted.');
- return redirect()->to('/products');
- }
Add Comment
Please, Sign In to add comment