Lugas Anegah Lugas Anegah - 1 year ago 67
PHP Question

how to select many data with array in laravel

my controller

public function lemari_surat_alpha(){
$show_dinas = dinas::all();
$show_surat = surats::all();
return view('frontend.daftar_surat_perdinas', compact('show_dinas','show_surat'));
}


this is daftar_surat_perdinas.blade.php

I don't know to show many data with array for selecting in Model

Answer Source

You're basically trying to group the results in alphabetical order within view. Why not arrange your results first and then display it.

Try this.

public function lemari_surat_alpha(){
    $show_dinas = dinas::all();

    // Uppercase alphabet keys collection
    $alphabets = collect(range('A', 'Z'))->flip();

    // Grouped results
    $results = surats::all()->groupBy(function($item) {
        return strtoupper($item->nama_surat[0]);
    });

    // Merge results with alphabets
    $show_surat = $alphabets->map(function($item, $key) use ($results) {
        return $results->has($key) ? $results->get($key) : null;
    });

    return view('frontend.daftar_surat_perdinas', compact('show_dinas','show_surat'));       
}

This will return grouped results. Like this

collection

then you can iterate over the collection in your blade file like this

@foreach ($show_surat as $alphabet => $surats)

    <div class="entry-image">
        <a href="#" class="entry-link" target="_blank">
            &nbsp;
            {{ $alphabet }}
            &nbsp;
        </a>
    </div>

    @if (is_null($surats))
        No Results
    @else
        <ul style="margin-left: 20px;">
            @foreach($surats as $surat)
                <li style="font-size: 18px;">{{ $surat->nama_surat }}</li>
            @endforeach
        </ul>
    @endif

@endforeach
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download