首页 > 解决方案 > 一个函数的用户变量,在另一个函数中

问题描述

我有一个 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
   }


   public function createTags($tags, $post_id)
    {

        $post_tags = PostTags::where('post_id', $post_id)->get();

        foreach ($post_tags as $post_tag) {
            Tags::where('id', $post_tag->tag_id)->delete();
        }

        PostTags::where('post_id', $post_id)->delete();

        foreach ($tags as $tag) {

            $old_tag=Tags::where('title',$tag)->first();

            if(isset($old_tag))
            {

                $pt = new PostTags();
                $pt->post_id = $post_id;
                $pt->tag_id = $old_tag->id;
                $pt->save();

            }

            else {
                $new_tag = new Tags();
                $new_tag->title = $tag;
                $new_tag->slug = Str::slug($tag);
                $new_tag->save();

                $pt = new PostTags();
                $pt->post_id = $post_id;
                $pt->tag_id = $new_tag->id;
                $pt->save();
            }
        }


   }

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

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

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

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

var_dump($tag);

紧接着

foreach ($tags as $tag)

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

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

标签: phplaravelfunctionvariableslaravel-5

解决方案


猜测 post item 模型有一个关系“标签”,你可以试试这个:

$tweet = $post_item->title . ' tags: ' . implode(', ', $post_item->tags->toArray());

此外,如果您只想在注释位置回显标签,请尝试以下操作:

echo implode(',', $item['categories']);

推荐阅读