84

I try to build a path for a model on laravel

I created a function in my model:

public function path()
{
    return App\Helper\GeneralController::getURL($this);
}

with dd(App\Helper\GeneralController::getURL($this)) test I got the right answer. (output is a URL)

but in view with the call: $article->path I get this error:

App\Article:: path must return a relationship instance.

What is wrong?

Neuron
  • 5,141
  • 5
  • 38
  • 59
Ali
  • 1,127
  • 1
  • 10
  • 23
  • What url you want to get by `getURL` ? Did you check [docs](https://laravel.com/docs/5.5/urls#accessing-the-current-url) may be it help. – Niklesh Raut Dec 09 '17 at 10:07

7 Answers7

156

You need to call it:

$article->path()

When you do $article->path, you're trying to use Eloquent relationship which you don't have.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • 3
    I actually was aware of this but simply didn't notice that I hadn't added "()" to my line of code. Thus, it was trying to access a property instead of executing a method. I was like "wtf am I doing wrong.." :'). Thanks for making me see the light. – minitauros Jul 20 '18 at 09:41
  • 1
    Exactly. Where there is an eloquent relationship when you called a method's name ONLY from Model, it returns the object with relationship. However when there isn't an eloquent relationship, for example in this case the url string, the method has to be called as an ordinary php `method()`. – seedme Feb 10 '21 at 23:21
27

I know this has already been answered and accepted. However, if the OP did want to use a property accessor rather than a method use the "get{property name}Attribute" syntax of Laravel to create a custom attribute.

Here is what it would look like for this specific case:

public function getPathAttribute()
{
    return App\Helper\GeneralController::getURL($this);
}

using this approach "path" can now be called as an attribute and will not be resolved to a relationship using the syntax:

$article->path;
pwyg
  • 689
  • 11
  • 8
  • Beautiful, along with all the other answers here there's the capacity to call it any way you could want but this is the reason I came to the thread. Thank you – Fi Horan Nov 25 '22 at 14:40
16

You're calling a relationship.

$article->path

To call the method, use '()', like so,

$article->path()
10

I faced that error when I forgot to write return before relation in the model!
check it out now!

Ali
  • 1,525
  • 1
  • 16
  • 27
6

path() is method not object element you need to call as method

$article->path();
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
1

Laravel 9 introduced a new way to define accessors/mutators within a model using Illuminate\Database\Eloquent\Casts\Attribute.

https://laravel.com/docs/9.x/eloquent-mutators#defining-an-accessor

public function path(): Attribute
{
    return new Attribute(fn () => GeneralController::getURL($this));
}
Cameron Wilby
  • 2,222
  • 1
  • 25
  • 35
0

For future visitors from Google, all the other answers can be applicable in certain scenarios, but you might want to also look if your method access modifier, if your method is protected and you try to call it you will be welcome with this error. You need change your method to public.

Amirmasoud
  • 590
  • 4
  • 11
  • 27