一个函数的用户变量,在另一个函数中

我有一个 Laravel 应用程序,其 PHP 代码如下:


   public function handle()

    {

                            $post_item->category_id = $source->category_id;

                            $post_item->featured = 0;

                            $post_item->type = Posts::TYPE_SOURCE;

                            $post_item->render_type = $item['render_type'];

                            $post_item->source_id = $source->id;

                            $post_item->description = is_array($item['description'])?'':$item['description'];

                            $post_item->featured_image = $item['featured_image'];

                            $post_item->video_embed_code = $item['video_embed_code'];

                            $post_item->dont_show_author_publisher = $source->dont_show_author_publisher;

                            $post_item->show_post_source = $source->show_post_source;

                            $post_item->show_author_box = $source->dont_show_author_publisher == 1 ? 0 : 1;

                            $post_item->show_author_socials = $source->dont_show_author_publisher == 1 ? 0 : 1;

                            $post_item->rating_box = 0;

                            $post_item->created_at = $item['pubDate'];

                            $post_item->views = 1;

                            $post_item->save();

                            $this->createTags($item['categories'], $post_item->id);


                           // This is where I want to add my echo

   }

   }

我试图在评论的地方之后回显标题和标签,但它无法提供正确的输出。我想知道我是否使用了正确的方法或者我的解决方法是完全错误的。


我在注释部分使用此代码:


$tweet = $post_item->title . ' tags: ' . $post_item->tags;

做了一些测试后,我意识到如果我使用


var_dump($tag);

紧接着


foreach ($tags as $tag)

在我的createTags 函数中,似乎所有标签都正确输出。


我想知道我是否可以将所有 $tags 存储在 createTag 函数的 foreach 中,在一个全局访问的变量下,该变量将在回显的初始句柄函数中使用。


HUWWW
浏览 124回答 2
2回答

侃侃无极

猜测 post item 模型有一个关系“标签”,你可以试试这个:$tweet = $post_item->title . ' tags: ' . implode(', ', $post_item->tags->toArray());此外,如果您只想在注释位置回显标签,请尝试以下操作:echo implode(',', $item['categories']);

尚方宝剑之说

尝试如果 $post_item->tags - 是数组然后$tweet = $post_item->title . ' tags: ' . implode(', ', $post_item->tags);如果 - 收集然后$tweet = $post_item->title . ' tags: ' . $post_item->tags->join(', ');
打开App,查看更多内容
随时随地看视频慕课网APP