Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
Laravel donde no

Necesito replicar esta consulta con Eloquent QueryBuilder:

 select * from table_name where type = 'custom_type' and not ( (topable_type = 'Car' and topable_id = 1) or (topable_type = 'Car' and topable_id = 2) )

El problema viene con la parte and not . Por lo que puedo ver, Laravel no admite la funcionalidad "whereNot".

Editar: intento de código:

 $deleteQuery = Item::where('type', '=', $type); foreach ($except as $exceptItem) { $deleteQuery = $deleteQuery->where(fn($query) => $query->where('topable_type', '!=', $exceptItem->type)->where('topable_id', '!=', $exceptItem->id)); } $deleteQuery->delete();

Edit2: con la ayuda de Tim Lewis, modifiqué mi intento anterior y ahora funciona:

 $deleteQuery = Item::where('type', $type); foreach ($except as $exceptItem) { $deleteQuery = $deleteQuery->where(fn($query) => $query->where('topable_type', $topableItem->type)->where('topable_id', $topableItem->id) , null, null, 'and not'); } $deleteQuery->delete();

No genera una consulta exacta como mencioné anteriormente, pero es bastante similar y funciona como se esperaba.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puedes hacer encadenado donde condición

 Item::where('type','custom_type')->where(function ($query){ $query->where(function ($query){ $query->where('topable_type','Car'); $query->where('topable_id',1); }); $query->orwhere(function ($query){ $query->where('topable_type','Car'); $query->where('topable_id',2); }); },null, null, 'and not')->get();

Referencia: https://github.com/laravel/ideas/issues/708

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!