Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mesqueeb's avatar

How to work with long PHP arrays in Laravel Views?

So I have a very long PHP array of data. e.g. 500 lines of this: ["Advance Business Concept", "UAE", "SS40"],

Now I want to display this array in the view with:

@foreach ($rows as $row)
    <p>{{ $row[0] }}</p>
@endforeach

But where can I best save the PHP array in my laravel, and how to retrieve it on this page?

(BTW, does google cache data for SEO if it's from a JSON file? I believe if its from a database it doesn't right?)

0 likes
7 replies
starmatt's avatar

You need to store the array in the session.

You can accomplish this when rendering your view (in a controller method for instance).

return view('path.to.view', ['rows' => $array]);

Or via a redirect:

return redirect('/your-route')->with(['rows' => $array]);

Edit: not sure my answer is actually pertinent to your issue, I think I misread your question, my bad!

mesqueeb's avatar

@starmatt Thank you!! So the best way is to give PHP variables to the view through the web routes.

Now my PHP array is over 500 lines, how can I refer this from another location in the web routes, without having to insert the complete array inside the web routes file?

(same for controller, if i'd use the controller, I'd still wouldn't want the 500+ array sitting in the controller...)

starmatt's avatar

Well, you could save it to your database, and then access it via a model or the DB facade, or create a function somewhere that returns the array, so you can just call getMyArray() for instance to retrieve it in your application, a little bit like Laravel's config files.

mesqueeb's avatar

@starmatt Exactly this is my question!

I wonder:

  1. if I save it in the DB, will google cache it for SEO?
  2. if I want to save it not in a DB, where would be a good location to save it? Where do I save my function?
starmatt's avatar
starmatt
Best Answer
Level 3
  1. Honestly I don't know much about SEO. But I can't think of why Google wouldn't cache it.

  2. That's up to you and what the architecture of your folders looks like. A default location could be in a Helper class, for instance in App/Helper/Helper.php. But again, that's up to you.

mesqueeb's avatar

@starmatt Dear Starmatt,

Sorry I'm still quite new at Laravel and the back end.

I had a look at the commands available to make classes in Laravel, but I couldn't find the helper class.

 make
  make:auth            Scaffold basic login and registration views and routes
  make:command         Create a new Artisan command
  make:controller      Create a new controller class
  make:event           Create a new event class
  make:job             Create a new job class
  make:listener        Create a new event listener class
  make:mail            Create a new email class
  make:middleware      Create a new middleware class
  make:migration       Create a new migration file
  make:model           Create a new Eloquent model class
  make:notification    Create a new notification class
  make:policy          Create a new policy class
  make:provider        Create a new service provider class
  make:request         Create a new form request class
  make:seeder          Create a new seeder class
  make:test            Create a new test class

Do you know how to create this class you mentioned?

Best regards,

starmatt's avatar

Yeah I know :) It would have to be a custom class, not something that's already part of Laravel. That's why it's up to you where to put it.

If you're new to php, check the video series here on Laracasts. If creating your own custom class is too complicated for you at this point I can give you an exemple you can use.

Please or to participate in this conversation.