我有一个订单表,我之前创建了这个表,但一段时间后我必须更改我的迁移。
这是更改前我的订单表:
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->bigInteger('price');
$table->enum('status', ['unpaid', 'paid', 'preparation', 'posted', 'recieved', 'canceled']);
$table->string('tracking_serial')->nullable();
$table->timestamps();
});
Schema::create('order_product', function (Blueprint $table) {
$table->unsignedBigInteger('product_id');
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->unsignedBigInteger('order_id');
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->integer('quantity');
$table->primary(['product_id', 'order_id']);
});
Run Code Online (Sandbox Code Playgroud)
这是更改后的订单表:
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedBigInteger('address_id');
$table->foreign('address_id')->references('id')->on('addresses')->onDelete('cascade');
$table->bigInteger('price');
$table->string('post_type');
$table->enum('status', ['unpaid', 'paid', 'preparation', 'posted', 'recieved', 'canceled']);
$table->string('tracking_serial')->nullable();
$table->primary(['user_id', 'address_id']);
$table->timestamps();
});
Schema::create('order_product', function (Blueprint $table) {
$table->unsignedBigInteger('product_id');
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->unsignedBigInteger('order_id');
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->integer('quantity');
$table->primary(['product_id', 'order_id']);
});
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我在ordersschema 中导入了 3 …
我有一个模型叫做BrandCodeModell:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class BrandCodeModell extends Model
{
use HasFactory;
public function brand()
{
return $this->belongsTo(Brand::class, 'brand_id', 'id');
}
public function modell()
{
return $this->belongsTo(Modell::class, 'modell_id', 'id');
}
public function codeModell()
{
return $this->belongsTo(CodeModell::class, 'code_id', 'id');
}
}
Run Code Online (Sandbox Code Playgroud)
这是Modell与 Model 的关系Brand:
public function modells()
{
return $this->belongsToMany(Modell::class)->using(BrandCodeModell::class)
->withPivot('code_id');
}
Run Code Online (Sandbox Code Playgroud)
当我尝试将新模型添加到数据库时,我收到此错误:
BadMethodCallException
Call to undefined method App\Models\BrandCodeModell::fromRawAttributes()
Run Code Online (Sandbox Code Playgroud)
这是我将模型保存到数据库的控制器:
$brand = Brand::where('id', $id)->first();
$brand->modells()->detach();
$modells = collect($request['modells']);
$modells->each(function ($item) use ($brand) …Run Code Online (Sandbox Code Playgroud)