lundi 23 novembre 2015

convert select DB::table to array on laravel

i want to change this code

 $data = array(
        'O' => 'Orange',
        'Y' => 'Yellow',
        'G' => 'Green',
        'B' => 'Blue',
        'I' => 'Indigo',
        'V' => 'Violet',
    );

with this code

 $d = DB::table('sps')
                ->select(array('sps.namasp'))
                ->where('namasp','like',$term)
                ->get();

Here is my full code on route

Route::get('getdata', function()
{
    $term = Input::get('term');
    $data = array(
        'SPION DEPAN' => 'Spion Depan',
        'SPION TENGAH' => 'Spion Tengah',
        'O' => 'Orange',
        'Y' => 'Yellow',
        'G' => 'Green',
        'B' => 'Blue',
        'I' => 'Indigo',
        'V' => 'Violet',
    );
    $return_array = array();

    foreach ($data as $k => $v) {
        if (strpos($v, $term) !== FALSE) {
            $return_array[] = array('value' => $v, 'id' =>$k);
        }
    }
    return Response::json($return_array);
});

basicly i try to find code for autocomplete on my blade. and i stack here. if you have any references for search autocomplete on laravel 5.1, give me the example or link. thanks before :)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire