0

I want to call $images values anywhere outside the loop.

$images = DB::table('images')->where('status', 'active')->get();
@foreach ($images as $img) {
    $title = $images->title;
    echo $title;
@endforeach
}

Output: Apple Mango Banana Pine Grapes

Now I want to call all these values of $title within a division which is outside the loop.

<div>
{{ $title }}
</div>
Anonymous Girl
  • 582
  • 8
  • 22
  • No @DirkJ.Faber mine is simple and straight forward, the one pointed by you is taking me here n there. – Anonymous Girl Jul 18 '21 at 15:20
  • Yes, it requires reading. For further information, please see [How to Ask](https://stackoverflow.com/questions/how-to-ask), and take the [tour](https://stackoverflow.com/tour). – hakre Jul 18 '21 at 15:21
  • Just read the question and answer and you'll understand what scope is and how to solve your problem. – Dirk J. Faber Jul 18 '21 at 15:28
  • 1
    Are you wanting to output the `title` individually, or all together in a single line? – Peppermintology Jul 18 '21 at 15:31
  • @Peppermintology in single line. – Anonymous Girl Jul 18 '21 at 15:32
  • You can achieve this without a `foreach` using the [`implode`](https://laravel.com/docs/8.x/collections#method-implode) method on the `$images` collection. I can't provide a better answer as some others have decided to close your question. – Peppermintology Jul 18 '21 at 15:39

0 Answers0