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

223
Visualizações
App\Models\ must return a relationship instance

I am trying to run some dynamic method calls based on the value of a database field. Some context: I have a model Anniversary and I want to display all upcoming anniversaries within the next x days. An anniversary has a date and a frequency. For example, monthly, quarterly, etc. Based on the frequency, I want to check for each anniversary if it is upcoming.

Here is my code so far:

$anniversaries = auth()->user()->anniversaries()->get();

$test = $anniversaries->filter(function ($anniversary) {
    $method = Str::of($anniversary->frequency)->camel();
    return ${$anniversary->$method}() == true;
});

dd($test);

The above works, when in the actual method I dd() something. But when returning true or false, I get the error:

App\Models\Anniversary::monthly must return a relationship instance

And in my model I just have a few methods like below, for testing:

public function monthly()
{
    return true;
}

public function quarterly()
{
    return false;
}

My only question is, I want to understand why I am getting this error and ofcourse any pointers in the right direction to get what I want to work. Thanks!

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

0

The following line creates an Illuminate\Support\Str object instead of a string. This causes the Method name must be a string error.

$method = Str::of($anniversary->frequency)->camel();

You can fix this by manually casting it to a string and invoking it directly:

$test = $anniversaries->filter(function ($anniversary) {
    $method = (string) (Str::of($anniversary->frequency)->camel());
    return $anniversary->$method() == true;
});
over 4 years ago · Santiago Trujillo Relatório

0

Throwing in my 2 cents for this as well. The Str::of(), which are "Fluent Strings" added in Laravel 7.x return an instance of Stringable:

https://laravel.com/api/8.x/Illuminate/Support/Stringable.html

For example:

dd(Str::of('monthly')->camel());
Illuminate\Support\Stringable {#3444
  value: "monthly"
}

To get the value of this, as a string and not an object, you can cast it (as shown in MaartenDev's answer), or call the __toString() method:

dd(Str::of('monthly')->camel()->__toString());
"monthly"

In your code example, that would simply be:

$method = Str::of($anniversary->frequency)->camel()->__toString();
return $anniversary->{$method}() == true;

Alternatively, you can just use the Str::camel() function to bypass this Stringable class:

$method = Str::camel($anniversary->frequency);
return $anniversary->{$method}() == true;

https://laravel.com/docs/8.x/helpers#method-camel-case

Hope that helps clear up some confusion 😄

over 4 years ago · Santiago Trujillo Relatório

0

you have issue in this part ${$anniversary->$method}(). if you access a function like property laravel models thinks its relation function. so replace with $anniversary->{$method}()

try this one

$anniversaries = auth()->user()->anniversaries()->get();

$test = $anniversaries->filter(function ($anniversary) {
    $method = Str::of($anniversary->frequency)->camel();
    return $anniversary->{$method}() == true;
});

dd($test);
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