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

214
Visualizações
Laravel 5 SQL Relations

I have some problem with a query in Laravel. I want to create a filter query by column in server table but I don't know how to do this.

Need help to modify the following line:

$data = $video->files()->with('server')->get();

Model: server

public function files()
{
    return $this->hasMany('App\Models\File', 'id', 'server_id');
}

Model: file

public function server()
{
    return $this->belongsTo('App\Models\Server', 'server_id', 'id')->ordered();
}

My current query return this: (but it returned all, I need return only FILE flitered by type = 6 which is server table) I don;t know how to do this. On screen below data about type server is on #relations array enter image description here

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use WhereHas. Takes the relation name and a closure as a parameter, where you can define sub query like functionality with the relation.

$data = $video->files()->with('server')->whereHas('server', function ($query) 
{
    $query->where('type', '6');
})->get();
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