Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

303
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda