Just add a new function total()
to your model:
public function total()
{
// You can access the model using $this
// example: $this->id
// ...
// Your logic to get the count
// ...
$this->total = $theGeneratedCount;
return $this;
}
$invoice = find(1)->total(); // returns a Invoice model
echo $invoice->total;
This way you could continue chaining. Else, you could just return the count, but then you won't be able to chain.
...
return $theGeneratedCount;
...
$invoice = find(1);
echo $invoice->total(); // returns the count
To get $invoice->total to work you need to specify a __get() magic method:
public function __get($name)
{
if(method_exists($this, $name) return $this->{$name}()
return parent::__get($name);
}