Hello i am trying to make a simple delete function but its showing an error
This is the code from the controller:
public function destroy($id)
{
$clientOrder = clientHasOrder::where('order_id',$id)->firstOrFail();
$clientOrder->delete();
return redirect('/')->with('msg','Order Deleted successfully!');
}
This is the model code:
class clientHasOrder extends Model
{
use HasFactory;
public $timestamps = false;
protected $fillable = [
'order_id',
'product_id',
'amount',
];
}
This is the migration file:
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');
});
}
And when i click delete button this is the error im getting:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause'
delete from
`client_has_orders`
where
`id` is null
showing this line in red: $clientOrder->delete();
When i change the name of column from order_id
to id
the code works but i dont want to call it id
>Solution :
try it without firstorfail() because you table dose not have an ID.
public function destroy($id)
{
$clientOrder = clientHasOrder::where('order_id', $id)->delete();
return redirect('/')->with('msg', 'Order Deleted successfully!');
}