0

I have a Laravel app, which the following PHP code:

   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();
            }
        }


   }

Im trying to echo the the title along with the tags, right after the commented place, but it fails to provide the correct output. I was wondering if Im using the correct way or my workaround is completely wrong.

Im using this code in the commented part:

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

After doing some tests, I realized that if I use

var_dump($tag);

right after

foreach ($tags as $tag)

at my createTags function, it seems that all tags are output correctly.

Im wondering if I can store all $tags inside the createTag function's foreach, under a globally accessed variable that would be used in the initial handle function echoed.

7
  • What error you are getting? Commented Dec 24, 2019 at 9:45
  • Your createTags function is not returning anything that's why you don't get any output. Commented Dec 24, 2019 at 9:48
  • @rkg it actually does echo the proper tags, if I include an echo command after foreach $tags as $tag. Commented Dec 24, 2019 at 10:02
  • @PrashantDeshmukh..... Im not getting an error. I want to echo each $tag created in createTags function. Commented Dec 24, 2019 at 10:03
  • What echo command you are using that works correctly? Commented Dec 24, 2019 at 10:08

2 Answers 2

1

Guessing the post item model has a relation "tags" you could try this:

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

Also if you would just like to echo the tags on the commented place, try this:

echo implode(',', $item['categories']);
Sign up to request clarification or add additional context in comments.

3 Comments

Great - thats what I was looking for. I actually modified it to: $hashtags = '#' . implode( ' #', $item['categories'] ); so they appear as hashtags. Is there a way to trim whitespaces in categories that contain two words along with a whitespace in the middle?
Ended up in using preg_replace for this scope - Thanks.
You're welcome. Not sure how your code looks right now but you could also use: array_map('trim', $item['categories']); Eventually change 'trim' into any function you desire.
0

try if $post_item->tags - is array then

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

if - collection then

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

5 Comments

Thanks for the answer. Tags are being generated from the createTags function I shared in the initial question. Adding the array solution returns: implode(): Invalid arguments passed
can you show output this code var_dump($post_item->tags);
It displays NULL - whereas the post has posts attached.
var_dump($tag) works fine, if I set an echo right after foreach ($tags as $tag) inside the createTags function tho
Im wondering if I can store all $tags inside the createTag function's foreach, under a globally accessed variable that would be used in the initial handle function echoed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.