Created
March 29, 2017 13:46
-
-
Save judahnator/c75fe8534c1050aa0139ea875c7e78b1 to your computer and use it in GitHub Desktop.
Maybe use something like this to wrap the relationship in an accessor. The nonExistantComments could be something like a JSON field in the database that holds the "not saved" data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Collection; | |
class Post extends Model { | |
private $nonExistantComments; | |
public function __construct() { | |
$this->nonExistantComments = collect([]); | |
} | |
public function comments() { | |
return $this->hasMany(Comments::class); | |
} | |
public function getCommentsAttribute(Collection $value) { | |
return $value->merge($this->nonExistantComments); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment