jeudi 22 octobre 2015

Get data from one table using one query and filter different rows into separate variables (Laravel 5.1)

I understand the question might not be very clear but here is my situation. I'm using laravel 5 and i'm developing a CRM system. I have placed Marital Status and Sex/Gender into one Lookup table. Whenever i get the values from the database and pass it to the view, i have two separate queries.

$sexes = DB::table('Lookups')
         ->where('ValueType', '=', 'Sex')->get();`

$marstatus = DB::table('Lookups')
         ->where('ValueType', '=', 'Marital Status')->get();`

return view('clients.edit',compact('client'))
        ->with('sexes', $sexes)
        ->with('marstatus ', $marstatus );

This code actually works and i am able to get both the marital status and sex/gender on my view.

So, here is my question

  1. Doesn't this mean that i am sending a query to the database twice which affects performance even if it is small
  2. Isn't there a way to query all the values from the lookup table in one query and filter out the values on the controller. So it can be something like

    $Lookups = DB::table('Lookups')

and then filter the $Lookups variable and assign it into two different variables ($sexes and $marstatus) based on my filter criteria. i.e ($sexes is for values that have ValueType = 'Sex' ...)

  1. Which one is better for performance. Sending a query twice or three times or just filtering the data on the controller.


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire