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,510 points
3,660 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,880 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 17, 2020 in Laravel by Niroj
• 82,880 points
23,198 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,880 points
5,296 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,880 points
3,689 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,880 points
2,898 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,880 points
818 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,880 points
756 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,880 points
3,137 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,880 points
10,277 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP