Symfony \ Component \ Debug \ Exception \ FatalThrowableError(E_ERROR)未找到类'Cart'

时间:2018-04-21 17:31:38

标签: php laravel eloquent migration laravel-eloquent

在产品上调用删除功能时,我遇到错误。删除功能工作正常,如果我注释掉我分离数据透视表的行,但是当删除产品时我也想删除数据透视表中的所有条目。有谁知道为什么会这样?

数据库已成功迁移和播种。

数据透视表迁移:

<?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');
}

1 个答案:

答案 0 :(得分:2)

您未在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()部分迁移中的表格与up()部分中的实际表名称不匹配。