Say you know the message id, then you can attach one cat as follows:
Post::find($post_id)->cats()->attach($cat_id);
Or attach some cats, like this:
$cat_ids = array(1,2,3,4); Post::find($post_id)->cats()->attach($cat_ids);
If you got the Post model object in a variable, say $ post:
$post->cats()->attach($cat_id); // Or with multiple $cat_ids = array(1,2,3,4); $post->cats()->attach($cat_ids);
If you have one category as a model object, say $ model:
$post->cats()->save($model);
Stay tuned for @Gadoma's answers. This is not the case, but if you want to add categories to a post that already has categories, you should use attach () instead of sync (). Sync () will delete all others that were not provided to it during use.
Editing:
Therefore, if you create new mail, you are probably doing something like this:
$post = new Post; $post->title = 'The title'; $post->something_else = 'Lorem'; $post->save();
source share