您好,我正在try 做一个简单的删除功能,但它显示一个错误
以下是来自控制器的代码:
public function destroy($id)
{
$clientOrder = ClientHasOrder::where('order_id',$id)->firstOrFail();
$clientOrder->delete();
return redirect('/')->with('msg','Order Deleted successfully!');
}
以下是型号代码:
class clientHasOrder extends Model
{
use HasFactory;
public $timestamps = false;
protected $fillable = [
'order_id',
'product_id',
'amount',
];
}
这是迁移文件:
public function up()
{
Schema::create('client_has_orders', function (Blueprint $table)
{
$table->string('order_id')->constrained();
$table->foreignId('product_id')->constrained();
$table->string('amount')->default('200');
});
}
当我单击删除按钮时,我收到以下错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause'
delete from
`client_has_orders`
where
`id` is null
用红色显示这条线:$clientOrder->delete();
当我将列的名称从order_id
更改为id
时,代码可以工作,但我不想将其命名为id