我有一個 Laravel 應(yīng)用程序,其 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 函數(shù)中,似乎所有標簽都正確輸出。我想知道我是否可以將所有 $tags 存儲在 createTag 函數(shù)的 foreach 中,在一個全局訪問的變量下,該變量將在回顯的初始句柄函數(shù)中使用。
2 回答
侃侃無極
TA貢獻2051條經(jīng)驗 獲得超10個贊
猜測 post item 模型有一個關(guān)系“標簽”,你可以試試這個:
$tweet = $post_item->title . ' tags: ' . implode(', ', $post_item->tags->toArray());此外,如果您只想在注釋位置回顯標簽,請嘗試以下操作:
echo implode(',', $item['categories']);
尚方寶劍之說
TA貢獻1788條經(jīng)驗 獲得超4個贊
嘗試如果 $post_item->tags - 是數(shù)組然后
$tweet = $post_item->title . ' tags: ' . implode(', ', $post_item->tags);如果 - 收集然后
$tweet = $post_item->title . ' tags: ' . $post_item->tags->join(', ');- 2 回答
- 0 關(guān)注
- 128 瀏覽
添加回答
舉報
0/150
提交
取消
