Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why the delete query execute Laravel

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!');
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading