What you get back from preg_match_all is a count of matches.
Its probably not the function you wanted. You just need preg_match
http://php.net/manual/en/function.preg-match.php
if($post)
{
preg_match('/#(\w+)/', $request->get('body'), $tagNames);
// $tagnames contains an array of results. $tagnames[0] is all matches
$tagIds = [];
foreach($tagNames[0] as $tagName)
{
//$post->tags()->create(['name'=>$tagName]);
//Or to take care of avoiding duplication of Tag
//you could substitute the above line as
$tag = Tag::firstOrCreate(['name'=>$tagName]);
if($tag)
{
$tagIds[] = $tag->id;
}
}
$post->tags()->sync($tagIds);
}