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

349
Visualizações
How to pass a part of Laravel Eloquent query to method

I want to use the different queries inside one method and I want to pass a part of query inside that.

My method looks like this:

static function methodName($partOfQuery)
{
   ModelName::where('...')->$partOfQuery->...;
}

And I want to do something like:

$partOfQuery = where('columnName', '>=', 5)->whereRaw('Other Condition');
self::methodName($partOfQuery);

But I was faced with this error: Call to undefined function App\Classes\ClassName\where()

Anyone could help me with this issue? Thanks

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

0

I think that the error is because where is called without an eloquent model class.

Something you can do with query builder is to call a function inside your where condition like:

SomeModel::where(function($query){
// do something
$query->where(...)
})
over 4 years ago · Santiago Trujillo Relatório

0

I doubt there is nothing exactly like what you described. But there are alternatives, if I understand correctly, you are trying to chain multiple methods.

If so, you can do the following:

In your model class:

static function getActiveBooks()
{
    return self::where('status', 'active');
}
public function getFeaturedBooks() {
    return $this->getActiveBooks()->where('featured', 'active');
}

Usage:

$activeBooks = (new Book())->getFeaturedBooks()->get();

There are multiple ways, you can also use scope as @levi described in the comment section , they are 2 sides of the same coin.

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