Question

I'm new to Laravel and MVC, and I'm struggling with when and where I should do things...

I have a state select, drop-down, field (CA, NY, TX, etc) that I'd like populated from a DB table. This DB table is big (3 million rows) as it contains info of every location in the USA. So my raw SQL looks like:

select distinct state_alpha,state_numeric
from location_code
order by state_alpha;

I will have to do this several times throughout all of my application. The table is also read only - the user will never insert or update it. I will eventually have similar queries - pulling from the same table - that'll return counties in a state, cities in a county, etc.

So my 1st question is: Should I create a model for something like this? I'm worried because I have no idea how the model works underneath the covers, and I don't want it needlessly reading from the table or prepping it to be written to or something else.

My next question: Barring your answer of my 1st question, could you please provide a basic example of how I'd accomplish this.

Sorry if I sound really stupid, but it's not like the documentation for Laravel is really that inclussive - it says what it needs to allow you to use it, but it doesn't cover how it works very well - so much of this is magic to me.

Was it helpful?

Solution 2

So my 1st question is: Should I create a model for something like this? I'm worried because I have no idea how the model works underneath the covers, and I don't want it needlessly reading from the table or prepping it to be written to or something else.

All you want to do is to abstract that query via some function and invoke on demand ensuring that query could run only once. It actually just an approach and has nothing to do with a framework.

The workflow would look like this,

  • A function/class method that fetch records should be prepared
  • When accessing fetched records, you have to ensure if they haven't been accessed before - call that function only once, and save its returned value. Next time on accessing check if that value is not null.
  • And finally render that

My next question: Barring your answer of my 1st question, could you please provide a basic example of how I'd accomplish this.

function fetchStates() {

  static $result = null;

  // If this function has never been called before, then invoke a query
  if ($result === null) {
     $result = DB::select('select distinct state_alpha,state_numeric from location_code order by state_alpha;');
  }

  return $result;
}

OTHER TIPS

Laravel Eloquent class represents implementaton of Object Relational Mapping pattern.It is a class that in a nutshell, converts your db declarative logic to Object Oriented code. By extending Eloquent with you own model e.g.

class MyModel extends Eloquent{};

you are bridging the gap between two paradigms. You could use Laravel Fluent e.g:

$product = DB:table("product")->get();

which is a step lower then Eloquent.

Think is it better to use row queries like this one:

SELECT * from Product:

or

$product = Product::all();

Last example is much more natural to Laravel and PHP OO approach.

These are all abstractions, that makes your db access much more easy to use. They are based on builtin PHP mysql driver. Of course there is down side to it - mostly performance but it is up to you to decide based on you criteria. Fetching milions of records can be resource consuming, so use some tool to measure performance while trying different approches.

Besides Eloquent documentation

You can take a look at source code

Source is not so simple, since whole ORM pattern implementations was the most challenging task while developing Laravel (Taylor Otwell quote).

So, at the end - use model for any kind of non trivial db access, it is here to help. You can expand everything by creating your own functions and helpers, but first use what is offered by Laravel.

PS I answered your question by providing you some guidelines and links which you can follow and learn more about Laravel DB access.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top