为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
HUWWW 2022-07-02 15:22:10
我有一个 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 中,在一个全局访问的变量下,该变量将在回显的初始句柄函数中使用。
查看完整描述

2 回答

?
侃侃无极

TA贡献2051条经验 获得超10个赞

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

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

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

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


查看完整回答
反对 回复 2022-07-02
?
尚方宝剑之说

TA贡献1788条经验 获得超4个赞

尝试如果 $post_item->tags - 是数组然后

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

如果 - 收集然后

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


查看完整回答
反对 回复 2022-07-02
  • 2 回答
  • 0 关注
  • 123 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号