Search code examples
laravelrecordslaravel-3

Laravel - Working with multiple records, how do I pull the data I need?


WARNING:: LARAVEL SUPER-BEGINNER, BE GENTLE::

In my controller:

public function get_index(){
$lease_rates = DB::table('lease_rates');
$this->layout->nest('content','admin.lease_rates', array('lease_rates' => $lease_rates, ));

In my lease_rates table:

   id   lease_year   class_letter   rate
    1      2012           A          635
    2      2012           B          648
   ...     ....          ...         ...
    5      2012           E          789
    6      2011           A          629

In my lease_rates.blade.php:

<input type="text" 
       name="{{ $lease_rates->rate->find(1) }}" 
       value="{{ $lease_rates->rate->find(1) }}">

<input type="text" 
       name="{{ $lease_rates->rate->find(2) }}" 
       value="{{ $lease_rates->rate->find(2) }}">

Obviously the above doesn't work. The end result that I want:

  • Sort the array sent to the page by lease_year, descending then by class_letter ascending.
  • Put the values of the rate for each id into text boxes where the textbox name is the id and the text displayed is the rate for said id.
  • After that works, I'll need to submit any changes made to the table
  • After that, I'll work on adding records

Thanks so much! Like I said, I'm new, but eager to learn (and even more eager to get this thing done)


Solution

  • Did you look at the docs for the Fluent Query Builder (or Eloquent ORM)? http://laravel.com/docs/database/fluent

    You could do something like this (not checked, but you should get the idea):

    //In your controller
    $lease_rates = DB::table('lease_rates')->order_by('lease_year', 'desc')->order_by('class_letter', 'desc')->get();
    
    //In your view
    @foreach($lease_rates as $lease_rate)
        <input type="text" name="{{ $lease_rate->id) }}" value="{{ $lease_rate->rate }}">
    @endforeach
    
    
    //Save controller
    foreach(Input::all() as $id => $rate){
        DB::table('lease_rates')->where('id', '=', $id)
        ->update(array('rate' => rate));
    }
    

    You could also use the Eloquent ORM, to make it even easier.