Skip to content

Instantly share code, notes, and snippets.

@TiagoSilvaPereira
Last active November 19, 2018 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TiagoSilvaPereira/7e1d2d1bee31451d7565a728a7257e4b to your computer and use it in GitHub Desktop.
Save TiagoSilvaPereira/7e1d2d1bee31451d7565a728a7257e4b to your computer and use it in GitHub Desktop.
Laravel: get the correct model on morphTo relationships
<?php
public function getTaskAttribute()
{
if($this->commentable_type == 'App\Task') return $this->commentable;
return null;
}
public function getProjectAttribute()
{
if($this->commentable_type == 'App\Project') return $this->commentable;
return null;
}
public function commentable()
{
return $this->morphTo();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment