admin管理员组

文章数量:1024637

I’m working on a feature to display teacher data grouped by category (guru_category). Below is the code I’m using:

public function showGuru(Request $request)
{
    $search = $request->input('search');
    $category = $request->input('category');

    $gurus = Guru::when($search, function ($query) use ($search) {
            return $query->where('nama_guru', 'like', '%' . $search . '%')
                         ->orWhere('guru_category', 'like', '%' . $search . '%')
                         ->orWhere('divisi_guru', 'like', '%' . $search . '%');
        })
        ->when($category, function ($query) use ($category) {
            return $query->where('guru_category', $category); // Filter berdasarkan kategori
        })
        ->get()
        ->groupBy('guru_category'); // Kelompokkan berdasarkan kategori

    return view('show.guru', compact('gurus', 'search', 'category'));
}

blade:

@foreach($gurus as $category => $categoryGurus) <!-- Iterasi setiap kategori -->
    <div class="flex flex-col mb-8">
        <!-- Tampilkan nama kategori -->
        <div>
            <h1 class="font-bold text-md mb-2 uppercase">{{ $category }}</h1> <!-- Nama kategori -->
        </div>

        <!-- Iterasi guru di dalam kategori -->
        <div class="grid grid-cols-2 md:grid-cols-5 mx-4 gap-2 space-y-4">
            @if($categoryGurus->isEmpty()) <!-- Jika tidak ada guru dalam kategori -->
                <p>No gurus available in this category.</p>
            @else
                @foreach($categoryGurus as $guru) <!-- Iterasi setiap guru dalam kategori -->
                    <div class="flex flex-col">
                        <!-- Gambar Guru -->
                        <img class="mb-2 rounded-lg" src="{{ asset('storage/' . $guru->foto_guru) }}" alt="{{ $guru->nama_guru }}">
                        
                        <!-- Detail Guru -->
                        <div class="text-container">
                            <h1 class="font-bold text-sm capitalize leading-4 tracking-tighter">{{ $guru->nama_guru }}</h1>
                            <p class="text-xs leading-3 opacity-50">{{ $guru->divisi_guru }}</p>
                        </div>
                    </div>
                @endforeach
            @endif
        </div>
    </div>
@endforeach

I expected all categories (guru_category) to display with the grouped teachers under each category.

What Happened Instead: Only one category is displayed on the page, even though the dd() output in the controller shows multiple categories with their respective data. Here is the result from dd($gurus):

Illuminate\Database\Eloquent\Collection {
    "Kepala Sekolah" => Collection [
        0 => Guru { nama_guru: "mary jane", guru_category: "Kepala Sekolah" },
    ],
    "Matematika" => Collection [
        0 => Guru { nama_guru: "John Doe", guru_category: "Matematika" },
    ],
    ...
}

Why is only one category displayed in the view? Am I iterating through the grouped data incorrectly, or is there something wrong with my logic? Any help would be appreciated!

I’m working on a feature to display teacher data grouped by category (guru_category). Below is the code I’m using:

public function showGuru(Request $request)
{
    $search = $request->input('search');
    $category = $request->input('category');

    $gurus = Guru::when($search, function ($query) use ($search) {
            return $query->where('nama_guru', 'like', '%' . $search . '%')
                         ->orWhere('guru_category', 'like', '%' . $search . '%')
                         ->orWhere('divisi_guru', 'like', '%' . $search . '%');
        })
        ->when($category, function ($query) use ($category) {
            return $query->where('guru_category', $category); // Filter berdasarkan kategori
        })
        ->get()
        ->groupBy('guru_category'); // Kelompokkan berdasarkan kategori

    return view('show.guru', compact('gurus', 'search', 'category'));
}

blade:

@foreach($gurus as $category => $categoryGurus) <!-- Iterasi setiap kategori -->
    <div class="flex flex-col mb-8">
        <!-- Tampilkan nama kategori -->
        <div>
            <h1 class="font-bold text-md mb-2 uppercase">{{ $category }}</h1> <!-- Nama kategori -->
        </div>

        <!-- Iterasi guru di dalam kategori -->
        <div class="grid grid-cols-2 md:grid-cols-5 mx-4 gap-2 space-y-4">
            @if($categoryGurus->isEmpty()) <!-- Jika tidak ada guru dalam kategori -->
                <p>No gurus available in this category.</p>
            @else
                @foreach($categoryGurus as $guru) <!-- Iterasi setiap guru dalam kategori -->
                    <div class="flex flex-col">
                        <!-- Gambar Guru -->
                        <img class="mb-2 rounded-lg" src="{{ asset('storage/' . $guru->foto_guru) }}" alt="{{ $guru->nama_guru }}">
                        
                        <!-- Detail Guru -->
                        <div class="text-container">
                            <h1 class="font-bold text-sm capitalize leading-4 tracking-tighter">{{ $guru->nama_guru }}</h1>
                            <p class="text-xs leading-3 opacity-50">{{ $guru->divisi_guru }}</p>
                        </div>
                    </div>
                @endforeach
            @endif
        </div>
    </div>
@endforeach

I expected all categories (guru_category) to display with the grouped teachers under each category.

What Happened Instead: Only one category is displayed on the page, even though the dd() output in the controller shows multiple categories with their respective data. Here is the result from dd($gurus):

Illuminate\Database\Eloquent\Collection {
    "Kepala Sekolah" => Collection [
        0 => Guru { nama_guru: "mary jane", guru_category: "Kepala Sekolah" },
    ],
    "Matematika" => Collection [
        0 => Guru { nama_guru: "John Doe", guru_category: "Matematika" },
    ],
    ...
}

Why is only one category displayed in the view? Am I iterating through the grouped data incorrectly, or is there something wrong with my logic? Any help would be appreciated!

Share Improve this question asked Nov 18, 2024 at 15:56 Moses Farrel RisgianMoses Farrel Risgian 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Taking a look at the result of the dd() output, I can deduce that the inner collection also uses a key => value structure as well. As such, I would advice that on the line where you are looping through categoryGurus, you iterate through taking into account each iteration is a key => value pair. Then your blade file would be like

@foreach($gurus as $category => $categoryGurus) <!-- Iterasi setiap kategori -->
    <div class="flex flex-col mb-8">
        <!-- Tampilkan nama kategori -->
        <div>
            <h1 class="font-bold text-md mb-2 uppercase">{{ $category }}</h1> <!-- Nama kategori -->
        </div>

        <!-- Iterasi guru di dalam kategori -->
        <div class="grid grid-cols-2 md:grid-cols-5 mx-4 gap-2 space-y-4">
            @if($categoryGurus->isEmpty()) <!-- Jika tidak ada guru dalam kategori -->
                <p>No gurus available in this category.</p>
            @else
                @foreach($categoryGurus as $key => $guru) <!-- Iterasi setiap guru dalam kategori -->
                    <div class="flex flex-col">
                        <!-- Gambar Guru -->
                        <img class="mb-2 rounded-lg" src="{{ asset('storage/' . $guru->foto_guru) }}" alt="{{ $guru->nama_guru }}">
                        
                        <!-- Detail Guru -->
                        <div class="text-container">
                            <h1 class="font-bold text-sm capitalize leading-4 tracking-tighter">{{ $guru->nama_guru }}</h1>
                            <p class="text-xs leading-3 opacity-50">{{ $guru->divisi_guru }}</p>
                        </div>
                    </div>
                @endforeach
            @endif
        </div>
    </div>
@endforeach

I’m working on a feature to display teacher data grouped by category (guru_category). Below is the code I’m using:

public function showGuru(Request $request)
{
    $search = $request->input('search');
    $category = $request->input('category');

    $gurus = Guru::when($search, function ($query) use ($search) {
            return $query->where('nama_guru', 'like', '%' . $search . '%')
                         ->orWhere('guru_category', 'like', '%' . $search . '%')
                         ->orWhere('divisi_guru', 'like', '%' . $search . '%');
        })
        ->when($category, function ($query) use ($category) {
            return $query->where('guru_category', $category); // Filter berdasarkan kategori
        })
        ->get()
        ->groupBy('guru_category'); // Kelompokkan berdasarkan kategori

    return view('show.guru', compact('gurus', 'search', 'category'));
}

blade:

@foreach($gurus as $category => $categoryGurus) <!-- Iterasi setiap kategori -->
    <div class="flex flex-col mb-8">
        <!-- Tampilkan nama kategori -->
        <div>
            <h1 class="font-bold text-md mb-2 uppercase">{{ $category }}</h1> <!-- Nama kategori -->
        </div>

        <!-- Iterasi guru di dalam kategori -->
        <div class="grid grid-cols-2 md:grid-cols-5 mx-4 gap-2 space-y-4">
            @if($categoryGurus->isEmpty()) <!-- Jika tidak ada guru dalam kategori -->
                <p>No gurus available in this category.</p>
            @else
                @foreach($categoryGurus as $guru) <!-- Iterasi setiap guru dalam kategori -->
                    <div class="flex flex-col">
                        <!-- Gambar Guru -->
                        <img class="mb-2 rounded-lg" src="{{ asset('storage/' . $guru->foto_guru) }}" alt="{{ $guru->nama_guru }}">
                        
                        <!-- Detail Guru -->
                        <div class="text-container">
                            <h1 class="font-bold text-sm capitalize leading-4 tracking-tighter">{{ $guru->nama_guru }}</h1>
                            <p class="text-xs leading-3 opacity-50">{{ $guru->divisi_guru }}</p>
                        </div>
                    </div>
                @endforeach
            @endif
        </div>
    </div>
@endforeach

I expected all categories (guru_category) to display with the grouped teachers under each category.

What Happened Instead: Only one category is displayed on the page, even though the dd() output in the controller shows multiple categories with their respective data. Here is the result from dd($gurus):

Illuminate\Database\Eloquent\Collection {
    "Kepala Sekolah" => Collection [
        0 => Guru { nama_guru: "mary jane", guru_category: "Kepala Sekolah" },
    ],
    "Matematika" => Collection [
        0 => Guru { nama_guru: "John Doe", guru_category: "Matematika" },
    ],
    ...
}

Why is only one category displayed in the view? Am I iterating through the grouped data incorrectly, or is there something wrong with my logic? Any help would be appreciated!

I’m working on a feature to display teacher data grouped by category (guru_category). Below is the code I’m using:

public function showGuru(Request $request)
{
    $search = $request->input('search');
    $category = $request->input('category');

    $gurus = Guru::when($search, function ($query) use ($search) {
            return $query->where('nama_guru', 'like', '%' . $search . '%')
                         ->orWhere('guru_category', 'like', '%' . $search . '%')
                         ->orWhere('divisi_guru', 'like', '%' . $search . '%');
        })
        ->when($category, function ($query) use ($category) {
            return $query->where('guru_category', $category); // Filter berdasarkan kategori
        })
        ->get()
        ->groupBy('guru_category'); // Kelompokkan berdasarkan kategori

    return view('show.guru', compact('gurus', 'search', 'category'));
}

blade:

@foreach($gurus as $category => $categoryGurus) <!-- Iterasi setiap kategori -->
    <div class="flex flex-col mb-8">
        <!-- Tampilkan nama kategori -->
        <div>
            <h1 class="font-bold text-md mb-2 uppercase">{{ $category }}</h1> <!-- Nama kategori -->
        </div>

        <!-- Iterasi guru di dalam kategori -->
        <div class="grid grid-cols-2 md:grid-cols-5 mx-4 gap-2 space-y-4">
            @if($categoryGurus->isEmpty()) <!-- Jika tidak ada guru dalam kategori -->
                <p>No gurus available in this category.</p>
            @else
                @foreach($categoryGurus as $guru) <!-- Iterasi setiap guru dalam kategori -->
                    <div class="flex flex-col">
                        <!-- Gambar Guru -->
                        <img class="mb-2 rounded-lg" src="{{ asset('storage/' . $guru->foto_guru) }}" alt="{{ $guru->nama_guru }}">
                        
                        <!-- Detail Guru -->
                        <div class="text-container">
                            <h1 class="font-bold text-sm capitalize leading-4 tracking-tighter">{{ $guru->nama_guru }}</h1>
                            <p class="text-xs leading-3 opacity-50">{{ $guru->divisi_guru }}</p>
                        </div>
                    </div>
                @endforeach
            @endif
        </div>
    </div>
@endforeach

I expected all categories (guru_category) to display with the grouped teachers under each category.

What Happened Instead: Only one category is displayed on the page, even though the dd() output in the controller shows multiple categories with their respective data. Here is the result from dd($gurus):

Illuminate\Database\Eloquent\Collection {
    "Kepala Sekolah" => Collection [
        0 => Guru { nama_guru: "mary jane", guru_category: "Kepala Sekolah" },
    ],
    "Matematika" => Collection [
        0 => Guru { nama_guru: "John Doe", guru_category: "Matematika" },
    ],
    ...
}

Why is only one category displayed in the view? Am I iterating through the grouped data incorrectly, or is there something wrong with my logic? Any help would be appreciated!

Share Improve this question asked Nov 18, 2024 at 15:56 Moses Farrel RisgianMoses Farrel Risgian 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Taking a look at the result of the dd() output, I can deduce that the inner collection also uses a key => value structure as well. As such, I would advice that on the line where you are looping through categoryGurus, you iterate through taking into account each iteration is a key => value pair. Then your blade file would be like

@foreach($gurus as $category => $categoryGurus) <!-- Iterasi setiap kategori -->
    <div class="flex flex-col mb-8">
        <!-- Tampilkan nama kategori -->
        <div>
            <h1 class="font-bold text-md mb-2 uppercase">{{ $category }}</h1> <!-- Nama kategori -->
        </div>

        <!-- Iterasi guru di dalam kategori -->
        <div class="grid grid-cols-2 md:grid-cols-5 mx-4 gap-2 space-y-4">
            @if($categoryGurus->isEmpty()) <!-- Jika tidak ada guru dalam kategori -->
                <p>No gurus available in this category.</p>
            @else
                @foreach($categoryGurus as $key => $guru) <!-- Iterasi setiap guru dalam kategori -->
                    <div class="flex flex-col">
                        <!-- Gambar Guru -->
                        <img class="mb-2 rounded-lg" src="{{ asset('storage/' . $guru->foto_guru) }}" alt="{{ $guru->nama_guru }}">
                        
                        <!-- Detail Guru -->
                        <div class="text-container">
                            <h1 class="font-bold text-sm capitalize leading-4 tracking-tighter">{{ $guru->nama_guru }}</h1>
                            <p class="text-xs leading-3 opacity-50">{{ $guru->divisi_guru }}</p>
                        </div>
                    </div>
                @endforeach
            @endif
        </div>
    </div>
@endforeach

本文标签: phpProperty namaguru does not exist on this collection instanceStack Overflow