我不知道出了什么问题,因为我对此很陌生。
// Product Model
class Product extends Model
{
use HasFactory;
public function store()
{
return $this->belongsTo(Store::class);
}
}
// Store Model
class Store extends Model
{
use HasFactory;
public function products()
{
return $this->hasMany(Product::class);
}
}
// Products table migration
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->float('price');
$table->string('description');
$table->timestamps();
$table->foreignId('store_id')->constrained()->onDelete('cascade');
});
// Stores table migration
Schema::create('stores', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('image_url');
$table->string('phone');
$table->timestamps();
});
When I run the migration, it gives me this error
我尝试更改“id”的数据类型,但仍然不起作用。我也尝试过
$table->foreign('store_id')->references('id')->on('stores')->onDelete('cascade');
但仍然不工作。
我想要的是一种关系,以便当我删除商店时,属于该商店的所有产品也会被删除。
Thanks ????