Reputation: 624
When calling a delete function on a product I get above error. The delete function works fine if i comment out the line where i detach the pivot table, however when deleting a product i would like to delete all entries in the pivot table as well. Does anyone know why this is happening?
The database has been migrated and seeded succesfully.
Pivot Table Migration:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
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');
}
}
Product Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['name', 'price', 'stock', 'short_description', 'long_description'];
public function orders() {
return $this->belongsToMany('App\Order', 'order_product', 'product_id', 'order_id');
}
public function carts() {
return $this->belongsToMany('App\Cart', 'cart_product', 'product_id', 'cart_id');
}
}
Delete Function:
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');
}
Upvotes: 0
Views: 1529
Reputation: 9952
You did not provide a full namespace in your belongsToMany
relationship.
Probably something like that (unless you have subfolders for models):
public function orders() {
return $this->belongsToMany('App\Order', 'order_product', 'product_id', 'order_id');
}
public function carts() {
return $this->belongsToMany('App\Cart', 'cart_product', 'product_id', 'cart_id');
}
Also, I would recommend adding this to your pivot migration:
Schema::create('order_product', function (Blueprint $table) {
// Also, you would need to make `order_id` and `product_id` unsigned,
// assuming your other `id` columns are `autoincrement` (which are unsigned by default)
$table->integer('order_id')->unsigned();
$table->integer('product_id')->unsigned();
$table->float('price');
$table->integer('amount');
$table->primary(array('order_id', 'product_id'));
$table->timestamps();
// Adds foreign key to orders
$table->foreign('order_id')
->references('id')
->on('orders')
// Automatically deletes the pivot, when related order is deleted
->onDelete('cascade');
// Adds foreign key to products
$table->foreign('product_id')
->references('id')
->on('products')
// Automatically deletes the pivot, when related cart is deleted
->onDelete('cascade');
});
Also your table in down()
part of migration mismatches the actual table name in the up()
part.
Upvotes: 2