Skip to content

Instantly share code, notes, and snippets.

@judahnator
Created March 29, 2017 13:46
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 judahnator/c75fe8534c1050aa0139ea875c7e78b1 to your computer and use it in GitHub Desktop.
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
<?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