1

I'm trying to loop through an object in an array in Laravel.

I made a foreach to loop through my $request->newTags what is an object and I just return the key. my goal is to access each tag_name in my request object what has an array with the multiple indexes what contain the tag_name.

foreach ($request->newTags as $tag) {
   return $tag;
 }

and i get my response enter image description here

how can I access each tag_name?

3 Answers 3

1

You are using return on each iteration which will exit the function first ooff.But it doesnt matter because newTags only contains one item, which is an array. So it sounds like you have an object attribute $tags->newtags which is an array containing an array. I bet that this will not throw an error for example:

echo $tags[0]["tag_name"];

Your problem is nesting.

Sign up to request clarification or add additional context in comments.

3 Comments

Yea I realized the mistake with return
Can you access your key if you go through the index of $tags? I would probably cast all to an array if $tags is mixed for simplicity btw.
Sorry for the late reply. I modified your answer a bit and it worked :)
0

Try this

foreach ($request->newTags as $tag) {
   return $tag.tag_name;
 }

4 Comments

Do you mean $tag->tag_name ?
@Yosef Yep try that. If that doesn't work try $tag['tag_name']
both don't work doing $tag->tag_name will return trying to get property of non-object and doing $tag['tag_name'] will return undefined index: tag_name
0
@foreach ($dates as $key => $value)
    @foreach ($humans as $human)
        {{ $human[$key]['date'] }}
    @endforeach
@endforeach

1 Comment

Please explain your answer - why does it work ?

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.