Symfony\Component\Debug\Exception\FatalThrowableError (E_ERROR) Class 'Cart' 未找到
Symfony\Component\Debug\Exception\FatalThrowableError (E_ERROR) Class 'Cart' not found
在产品上调用删除功能时出现上述错误。如果我注释掉分离数据透视表 table 的行,删除功能可以正常工作,但是在删除产品时,我也想删除数据透视表 table 中的所有条目。有谁知道为什么会这样?
数据库已成功迁移和播种。
透视Table迁移:
<?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');
}
}
产品型号:
<?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');
}
}
删除函数:
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');
}
您没有在 belongsToMany
关系中提供完整的命名空间。
可能是这样的(除非你有模型的子文件夹):
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');
}
此外,我建议将此添加到您的枢轴迁移中:
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');
});
此外,迁移的 down()
部分中的 table 与 up()
部分中的实际 table 名称不匹配。
在产品上调用删除功能时出现上述错误。如果我注释掉分离数据透视表 table 的行,删除功能可以正常工作,但是在删除产品时,我也想删除数据透视表 table 中的所有条目。有谁知道为什么会这样?
数据库已成功迁移和播种。
透视Table迁移:
<?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');
}
}
产品型号:
<?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');
}
}
删除函数:
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');
}
您没有在 belongsToMany
关系中提供完整的命名空间。
可能是这样的(除非你有模型的子文件夹):
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');
}
此外,我建议将此添加到您的枢轴迁移中:
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');
});
此外,迁移的 down()
部分中的 table 与 up()
部分中的实际 table 名称不匹配。