[英]Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Class 'APP\Mail\Reverify' not found
[英]Symfony\Component\Debug\Exception\FatalThrowableError (E_ERROR) Class 'Cart' not found
在產品上調用刪除功能時,出現上述錯誤。 如果我注釋掉分離數據透視表的那一行,則delete函數可以正常工作,但是在刪除產品時,我也要刪除數據透視表中的所有條目。 有人知道為什么會這樣嗎?
數據庫已成功遷移並播種。
數據透視表遷移:
<?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()
部分中的表與up()
部分中的實際表名不匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.