0

I'm trying to get sum of columns in relationship model but i get this error

Call to a member function addEagerConstraints() on float

Code

model

public function cableLentgh()
{
    return $this->links()->sum('cable_length');
}

Logic

  1. my model has relationship to link model like this
public function links()
{
    return $this->hasManyThrough(Link::class, Segment::class, 'hthree_id', 'segment_id');
}
  1. in links table I have column named cable_length where numbers are stored
  2. now I want to have SUM of those numbers.

Any idea?

Update

full error detail

exception: "Error"
file: "C:\laragon\www\web\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php"
line: 578
message: "Call to a member function addEagerConstraints() on float"
mafortis
  • 6,750
  • 23
  • 130
  • 288
  • What have you tried to debug this? Can you share the full and exact error message, which should at least contain the file and line throwing that error, if not also the stacktrace – Nico Haase Apr 14 '20 at 08:10
  • plz post the code that u use cableLength. – TsaiKoga Apr 14 '20 at 08:15
  • I think the problem is you are using some code like `with('cableLentgh'` – TsaiKoga Apr 14 '20 at 08:22
  • @TsaiKoga yes in my controller i am trying to get this model function in `with()` that's why in first place i added this function to my model – mafortis Apr 14 '20 at 09:58
  • @mafortis my answer may help u here: https://stackoverflow.com/questions/59897448/call-to-a-member-function-addeagerconstraints-on-string/59897724#59897724 – TsaiKoga Apr 14 '20 at 10:00
  • @TsaiKoga sorry your sample code is using morphable table mine is `hasManyThrough` how should i change it? – mafortis Apr 14 '20 at 10:03
  • I think the problem is `with` need the relationship. however, `cableLentgh()` return a float. and the error occurs. So if you want to use `with()`, the cableLentgh() need to return relationship. not just a number. – TsaiKoga Apr 14 '20 at 10:05
  • @TsaiKoga in that case i already have my links relationship returning then having `cableLentgh()` would be useless i guess. Well that way i think best way would be doing `sum` in front-end based on `links()` relation – mafortis Apr 14 '20 at 10:08

3 Answers3

0

If you relationship is correct then you should get the collection of links by the following call:

$this->link;

If it returns the collections, you can then run sum method of collection as below:

public function cableLentgh()
{
    return $this->links->sum('cable_length'); //Notice () removed
}

I know this might not be the exact answer for your problem as you are trying to get the sum by query which is better option. As it's not working, then using collection is also an option that may solve your problem.

Imran
  • 4,582
  • 2
  • 18
  • 37
  • thanks but it returns `Call to a member function addEagerConstraints() on int` – mafortis Apr 14 '20 at 08:03
  • In that case you should be confirm that the relationship is not working correctly. have you tried to see what the return of `$this->link`? – Imran Apr 14 '20 at 12:49
0

For me you should use the variable, and not the function link :

public function cableLentgh()
{
    return $this->links->sum('cable_length');
}
Vincent Decaux
  • 9,857
  • 6
  • 56
  • 84
  • it returns `Call to a member function addEagerConstraints() on int` – mafortis Apr 14 '20 at 08:03
  • Ok, but `$this->links` should return a collection, be sure of your relations, and test links to understand what is returned – Vincent Decaux Apr 14 '20 at 08:06
  • if i use `$this->links()` it return correct data. if `$this->link` it return error `Method Illuminate\Database\Eloquent\Collection::addEagerConstraints does not exist.` – mafortis Apr 14 '20 at 08:07
  • as of error seems this is what it's mean https://stackoverflow.com/a/48129928/8490993 – mafortis Apr 14 '20 at 08:13
0

I think it has something to do with the relationship ids',I would try to specify like in hasManyThrough documentation

class Country extends Model
{
    public function posts()
    {
        return $this->hasManyThrough(
            'App\Post',
            'App\User',
            'country_id', // Foreign key on users table...
            'user_id', // Foreign key on posts table...
            'id', // Local key on countries table...
            'id' // Local key on users table...
        );
    }
}
Siba Al
  • 357
  • 1
  • 14