Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

297
Vistas
How to check hasMany relation record is empty in eloquent laravel

I need to get those record who has not join with has many table my query like this

$result = Recipes::select('id')->where("name", 'like', '%' . $item . '%')
                    ->with(['categoryItem'])
                    ->get();

And my output is

Array
(
    [0] => Array
        (
            [id] => 1
            [category_item] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [foodable_id] => 1
                            [foodable_type] => Recipes
                            [created_at] => 2017-04-14 16:18:22   
                        )

                )

        )

    [1] => Array
        (
            [id] => 4
            [category_item] => Array
                (
                )

        )

)

I want to get just those record who has empty category_item detail. in bellow example i want to get index 1 record so how can i give condition with query

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

If I understood you correctly and you want to get recipies which don't have categoryItem, you want to use doesntHave() method:

Recipes::where('name', 'like', '%'.$item.'%')
       ->doesntHave('categoryItem')
       ->get();

I assume that categoryItem is defined relation since you're trying to use it in with() method.

over 4 years ago · Santiago Trujillo Denunciar

0

Hello Maybe this can help . Dont forget to add on top of your file :

use DB;

And your result will be something like this

$result = Recipes::select('id')->where("name", 'like', '%' . $item . '%') ->whereHas('categoryItem', function($q) { $q->where(DB::raw('count(*)),'>', 0); })->get();

I did't tested the count function hope this works, Good luck with it

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda