Laravel - Undefined variable: modules

Zak :

I am trying to create a new module - returning the following error:

Undefined variable: modules (View: /Users/kerrymckinney/Desktop/LearningHub/resources/views/admin/module/index.blade.php)

index.blade.php;

@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row justify-content-center">
        <div class="col-md-8">
            <p>

                <a href="{{ route('admin.modules.create') }}"><button type="button" class="btn btn-success">Create Module</button></a>

            </p>
            <div class="card">
            <div class="card-header">Modules</div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table"> 
                        <thead>
                          <tr>
                            <th>ID</th>
                            <th>Module Title</th>
                            <th>Course Title</th>
                            <th></th>
                            <th></th>
                            <th></th>
                          </tr>
                        </thead>
                        <tbody>
                        @foreach($modules as $module)
                            <tr>
                            <th scope="row">{{ $module->id }}</th>
                            <td>{{ $module->title }}</td>
                            <td>{{ $module->course->title ?? ''}}</td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
        </div>
        </div>
        </div>
</div>
@endsection

ModulesController;

<?php

namespace App\Http\Controllers\Admin;

use App\User; 
use App\Role;
use App\Course;
use App\Module;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ModulesController extends Controller
{
    public function index(Request $request)
    {
        $modules = new module();

        if ($request->input('course_id')) {
                $modules = $modules->where('course_id', $request->input('course_id'));
        } $modules = $modules->get(); //passing modules with course_id through

        //$modules = $modules->all();
        return view('admin.module.index', compact('modules'));
    }

    public function create()
    {
        $courses = Course::all()->pluck('title', 'id');

        return view('admin.module.create', compact('courses'));
    }

    public function store(Request $request)
    {
        $module = Module::create($request->all());

        return view('admin.module.index', ['course_id' => $request->id]); //redirects to correct route by adding course_id in parameter 

    }

}    

I am not sure what I have done wrong, can anyone help? I am new to laravel.

Thanks.

Foued MOUSSI :

You may fix it by doing

public function index(Request $request) {

        if ($request->filled('course_id')) {

             $modules = Module::where('course_id', $request->input('course_id'))->get();
        } else {

             $modules = Module::all();
        }

        return view('admin.module.index', compact('modules'));
}

And in your blade you may use @forelse directive

What do you do when you need to show a loop in Blade with foreach, but the list might be empty? You probably write if-else statement around it, right?

There’s a “magic” loop structure called @forelse

The @foresee loop in essence is exactly the same as @foreach with one extra element, the else statement. The primary use for this to use when you have an empty array

@forelse($modules as $module)
      <tr>
          <th scope="row">{{ $module->id }}</th>
          <td>{{ $module->title }}</td>
          <td>{{ $module->course->title ?? ''}}</td>
      </tr>
@empty
<tr><td colspan="3"> No Records found </td></tr>
@endforelse

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=197948&siteId=1