Error Relationship method must return an object of type Illuminate Database Eloquent Relations Relation

0 votes

I have a Product model

class Product extends Model
{
    ...

    public function prices()
    {
        return $this->hasMany('App\Price');
    }

    ...
}

I want to add a function which will return the lowest price, and in controller I can get the value using:

Product::find(1)->lowest;

I added this in Product model:

public function lowest()
{
    return $this->prices->min('price');
}

but I got an error saying:

Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

And if I use Product::find(1)->lowest();, it will work. Is it possible to get Product::find(1)->lowest; to work?

Dec 1, 2020 in Laravel by kartik
• 37,490 points
106 views

1 answer to this question.

0 votes

Hello @kartik,

When you try to access a function in the model as a variable, laravel assumes you're trying to retrieve a related model. They call them dynamic properties. What you need instead is a custom attribute.

add following method to your model:

public function getLowestAttribute()
{
    //do whatever you want to do
    return 'lowest price';
}

Now you should be able to access it like this:

Product::find(1)->lowest;

Hope it helps!!

answered Dec 1, 2020 by Niroj
• 82,680 points

Related Questions In Laravel

0 votes
1 answer
0 votes
1 answer

I get this error > Trying to get property of non-object . How to fix it?

Hello @aakash, If you want to save grades ...READ MORE

answered Aug 16, 2020 in Laravel by Niroj
• 82,680 points
6,823 views
0 votes
1 answer

Error:The Response content must be a string or object implementing __toString(), "boolean" given.

Hello @kartik, Your response must return some sort ...READ MORE

answered Sep 24, 2020 in Laravel by Niroj
• 82,680 points
959 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,680 points
860 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,680 points
1,301 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,680 points
162 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,680 points
166 views
0 votes
1 answer

Error:Object of class Illuminate\Database\MySqlConnection could not be converted to string

Hello @kartik, The use keyword is what you need: $id = ...READ MORE

answered Sep 24, 2020 in Laravel by Niroj
• 82,680 points
439 views
0 votes
1 answer

What is the use of the Eloquent cursor() method in Laravel?

Hello, The cursor method allows us to iterate ...READ MORE

answered Mar 20, 2020 in Laravel by Niroj
• 82,680 points
3,209 views