Sabtu, 21 Juni 2014

An effective way to pass the data into the view in Laravel

To make it easier to digest this post, I use a pagination case.

Normally, you will have this variable request for pagination;
$page = Input::get('page'); 
$perpage = Input::get('perpage'); 
$sidx = Input::get('sidx');   //order field
$sord = Input::get('sord'); //order direction

Next thing you want to do is passing the data into view, right?
And in normal way, most of coder write code like below;
return View::make('page_search')
    ->with('page',$page)
    ->with('perpage',$perpage)
    ->with('sidx',$sidx)
    ->with('sord',$sord);

And retrieving the data like this
<input type="hidden" name="page" value="{{ $page }}" >
<input type="hidden" name="perpage" value="{{ $perpage }}" >
<input type="hidden" name="sidx" value="{{ $sidx }}" >
<input type="hidden" name="sord" value="{{ $sord }}" >


All above code look like this;
Controller
public static function getSearch()
{
    $page = Input::get('page'); 
    $perpage = Input::get('perpage'); 
    $sidx = Input::get('sidx');   //order field
    $sord = Input::get('sord'); //order direction

    $search_data = "your eloquent query here related to your page, perpage, sidx, sord";

    //passing the data into view
    return View::make('page_search')
        ->with('search_data', $search_data)
        ->with('page',$page)
        ->with('perpage',$perpage)
        ->with('sidx',$sidx)
        ->with('sord',$sord);    
}

View
{{ Form::open(.......) }}
<input type="hidden" name="page" value="{{ $page }}" >

<input type="hidden" name="perpage" value="{{ $perpage }}" >
<input type="hidden" name="sidx" value="{{ $sidx }}" >
<input type="hidden" name="sord" value="{{ $sord }}" >
<input type="submit" value="Search">
{{ Form::close() }}


Well, nothing wrong with above code. But you can do better with this;
Controller
public static function getSearch()
{
    $page = Input::get('page'); 
    $perpage = Input::get('perpage'); 
    $sidx = Input::get('sidx');   //order field
    $sord = Input::get('sord'); //order direction

    $search_data = "your eloquent query here related to your page, perpage, sidx, sord";


    /* YOU CAN DO LIKE THIS
    $form['page'] = $page;
    $form['perpage'] = $perpage;
    $form['sidx'] = $sidx;
    $form['sord'] = $sord;
    */

    /* OR LIKE THIS */
    $form = array('page' => $page, 'perpage' => $perpage, 'sidx' => $sidx, 'sord' => $sord);
   

    //passing the data into view
    return View::make('page_search')
        ->with('search_data', $search_data)
        ->with('form',$form);    
}

View
{{ Form::open(.......) }}
<input type="hidden" name="page" value="{{ $form['page'] }}" >

<input type="hidden" name="perpage" value="{{ $form['perpage'] }}" >
<input type="hidden" name="sidx" value="{{ $form['sidx'] }}" >
<input type="hidden" name="sord" value="{{ $form['sord'] }}" >
<input type="submit" value="Search">
{{ Form::close() }}



As you can see above, you can use an array for your data. :)

Tidak ada komentar:

Posting Komentar

Cara Mengetahui Besar Database PostgreSQL Tanpa Mendownloadnya

Berikut adalah langkah-langkah untuk mengetahui ukuran semua database di instance PostgreSQL yang berjalan di dalam kontainer Docker: 1. Men...