Reputation: 1544
Why calling a method with parenthesis in a template causes error, but without doesn't? How would I pass a parameter then?
Error:
Call to undefined method Illuminate\Database\Query\Builder::test()
index.blade.php
...
<p class="product-price-old"><s>{{ $product->test("123") }}</s></p>
...
Product.php
...
public function getTestAttribute($value)
{
return $value;
}
...
but if I do something like this it works fine:
index.blade.php
...
<p class="product-price-old"><s>{{ $product->test }}</s></p>
...
Product.php
...
public function getTestAttribute()
{
return "123";
}
...
Upvotes: 0
Views: 4305
Reputation: 126
If you are assigning an instance of Product.php (which should be a model of a product table in your database) to a blade variable, and your Product table has a column called 'test' , Then you're just printing that value from your model and the instance of Product.php does not have a test() method in it. You can just retrieve the value of test
attribute.
If you want to pass parameters to a function in your model, Use a different name for your function. You can not pass parameters to accessor
s in Laravel.
Upvotes: 2
Reputation: 111829
The getTestAttribute
is accessor, so it's not a real function. It rather shows how you can get attribute of model.
That's why for getTestAttribute method you should use:
$product->test
Upvotes: 1