【发布时间】:2023-01-30 20:51:48
【问题描述】:
你好,我正在尝试制作一个简单的删除功能,但它显示错误
这是来自控制器的代码:
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
【问题讨论】:
标签: laravel