Authorship is polymorphic, so any model can write posts — a User, a Team, an Organization. Add the HasPosts trait to each author model:
use Illuminate\Foundation\Auth\User as Authenticatable;
use RoundlyConsulting\Posts\Concerns\HasPosts;
final class User extends Authenticatable
{
use HasPosts; // $user->posts (morphMany)
}Pass the author to the Posts::draft() builder with by(), attach it through the morph relation, or give CreatePostData an authorType and authorId. The posts() relation goes through the configured post model, so post scopes chain straight off it:
use RoundlyConsulting\Posts\Facades\Posts;
$post = Posts::draft()->title('en', 'Hello world')->by($user)->save(); // polymorphic author
// Or on the model:
$post->author()->associate($team);
$post->save();
$post->author; // the author model (User, Team, …)
$user->posts()->published()->latest('published_at')->get();Author options
- author.morph-name — the relation name and column prefix (author_type / author_id). Default author.
- author.nullable — whether a post may exist without an author. Default true.
- key_type — the author_id column type; it must match your author model’s key (bigint, uuid or ulid).
- json-ld.author-attribute — the author attribute used as the JSON-LD author name. Default name.
Show your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.