How Can I load Data From Database When Class Is Selected?

  laravel
 <div class="float-right">
                    <form>
                        <div class="form-group row">
                            <div class="col-sm-12">

                                <select class="form-control" id="select_id" >
                                     <option> Choose Class </option>
                                    @foreach($allclass as $classs)
                                     <option value="{{$classs->id}}"> {{$classs->class}} </option>
                                        @endforeach
                                </select>

                            </div>
                        </div>
                    </form>

                </div>

This is Where user choose Class and when user choose the class i want to view data as per the class .

class SectionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    public function index(Clas $clas)
    {
        $allclass = Clas::all();
        $sections = $clas->sections;
        return view('admin.section.index')
            ->with('i',$i = 1)
            ->with('sections',$sections)
        ->with('allclass',$allclass);
    }

This is my section Controller

   public function clas(){
        return $this->belongsTo(Clas::class);
    }

This is Section Model where i keep realtion between clas and section .

 public function up()
    {
        Schema::create('sections', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('section');
            $table->string('category');
            $table->string('capacity');
            $table->integer('clas_id');
            $table->integer('teacher_id');
            $table->text('note');
            $table->softDeletes();
            $table->timestamps();
        });
    }

This is my sectios db .

  <table id="userTable" class="table table-bordered table-striped table-vcenter js-dataTable-full-pagination">
                    <thead>
                    <tr>
                        <th class="text-center">S.n</th>
                        <th class="d-none d-sm-table-cell">Section</th>
                        <th class="d-none d-sm-table-cell">Category</th>
                        <th class="d-none d-sm-table-cell">Capacity</th>
                        <th class="d-none d-sm-table-cell">Teacher Name</th>
                        <th class="d-none d-sm-table-cell">Note</th>

                        <th class="text-center">Action</th>
                    </tr>
                    </thead>
                    <tbody>

                    @foreach($sections as $s)
                        <tr id="table-data">
                            <td class="d-none d-sm-table-cell" width="8%">{{1}}</td>
                            <td class="d-none d-sm-table-cell" width="15%">{{$s->section}}</td>
                            <td class="d-none d-sm-table-cell" width="15%"> {{$s->category}}</td>
                            <td class="d-none d-sm-table-cell" width="15%"> {{$s->capacity}}</td>
                            <td class="d-none d-sm-table-cell" width="15%">
                                {{$s->teacher->user->name}}
                            </td>
                            <td class="d-none d-sm-table-cell" width="15%"> {{$s->note}}</td>
                            <td class="d-none d-sm-table-cell" width="20%">
                                <form class="" action="{{ route('section.destroy',$s->id) }}" method="post">
                                    @csrf
                                    @method('DELETE')
                                    <a href="{{$s->trashed()?route('section.restore',$s->id):route('section.edit',$s->id)}}">
                                        <button type="button" class="btn btn-warning mr-5 mb-5 " title="{{ $s->trashed()?'Recover':'Edit'}}">
                                            <i class="{{$s->trashed()?'fa fa-recycle':'fa fa-edit'}}"></i>
                                        </button>
                                    </a>


                                    <button type="submit" class="btn btn-danger mr-5 mb-5" title="{{ $s->trashed()?'Delete':'Trash'}}">
                                        <i class="{{$s->trashed()?'fa fa-trash':'fa fa-minus-circle'}}"></i>
                                    </button>


                                </form>
                            </td>

                        </tr>

                        @php $i++ @endphp

                    @endforeach
                    </tbody>
                </table>

This is my table for viewing section only when class is selected .
how can i View sections only when some class is selected .

Sorry For my english .

Now I want to view section only when class is selected. How can i do that ?

Source: Laravel StackOverflow

Leave a Reply