Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!