robclancy / presenter

Decorate your objects using presenters. Primarily to keep presentation logic out of your models.
MIT License
345 stars 38 forks source link

Fatal error Method Robbo\Presenter\View\View::__toString() must not throw an exception #13

Closed newtonianb closed 11 years ago

newtonianb commented 11 years ago

I have a method in my model

  public function posts() {
    return Post::where('receiver_id', $this->id);
  }

From my view when I call $user->posts I get

PHP Fatal error: Method Robbo\Presenter\View\View::__toString() must not throw an exception

And it hangs my entire apache on windows.

The only way I got this to work is if I change my posts function in the model to

  public function posts() {
    return Post::where('receiver_id', $this->id)->get();
  }

and my call in the view to $user->posts()

robclancy commented 11 years ago

This has nothing to do with the presenter. You are creating an exception because you are using eloquent wrong. You would get the same error without the presenter just with Illuminate\View\View::__toString() instead. Your posts method is a query builder instance and not a relationship therefore it doesn't have getResults which is what relationships use. Which I expect is what your exception actually is, call to undefined method getResults.

robclancy commented 11 years ago

Make your method...

public function posts()
{
    return $this->hasMany('Post', 'receiver_id');
}

or something similar.

newtonianb commented 11 years ago

ah ok thanks!