KaziBablu
KaziBablu

Reputation: 533

Change value with dropdown in Laravel

I want yourenter image description here suggestion If I change my dropdown select options then I want to change my table value for your better understand I am uploading a picture.Thanks

I am uploading my controller code

 public function index()
{
    $allRoom =AllocateClassroom::with('course','department')->paginate(10);

    //dd($allRoom);
    return view('Admin.allocateClassrooms.index',['allRoom'=>$allRoom]);

}

Here is my view code

 {{Form::label('department','Department')}}
            <select name="department" id="department" class="form-control">
                <option value=" ">----Select Department-----</option>
                @foreach($allRoom as $value)
                    <option value="{{$value->id}}">{{$value->department->name}}</option>
                @endforeach
            </select>

                    <br>
                        <div class="table-responsive">
                          <table class="table table-bordered">
                                  <tr class="success">
                                      <th>ID</th>
                                      <th>Course Code</th>
                                      <th>Course Name</th>
                                      <th>Schedule Info</th>
                                  </tr>

                                  <tr class="info" id="info">
                                      <td class="success" >{{$value->id}}</td>
                                      <td class="success" >{{$value->course->code}}</td>
                                      <td class="success" >{{$value->course->name}}</td>
                                      <td class="success" >{{$value->   Room_No}}
                                          {{$value->    date}}
                                          {{$value->    start_time}}
                                          {{$value->    end_time}}


                                      </td>
                                  </tr>
                          </table>
                        </div>

Upvotes: 0

Views: 5731

Answers (2)

fetty
fetty

Reputation: 25

You're trying to get a property of a non-object. Change {{$value->id}} to {{$value['id']}} in the file that gives your error.

Upvotes: 0

This is the way i would do it:

First, i add a form wrapping the select so when a user changes its value the page reloads with the department selected

<form method="get" id="department-form">
    <select name="department" id="department" class="form-control" onchange="document.getElementById('department-form').submit()">
        <option value=" ">----Select Department-----</option>
        @foreach($allRoom as $value)
            <option value="{{$value->id}}" @if($value->id == $department) selected @endif>{{$value->department->name}}</option>
        @endforeach
    </select>
</form>

And in my controller i would have this:

public function index( Request $request)
{
    $department = null;
    if($request->department) $department = $request->department;
    $allRoom =AllocateClassroom::with('course','department')->whereHas('department', function($query) use($department){
        if($department) $query->where(id, $department);
    })->paginate(10);

    return view('Admin.allocateClassrooms.index',['allRoom'=>$allRoom, 'department' => $department]);

}

Here I get my department from the request and query the data with it

Update

Sorry, last code was getting all rooms and the departments that met the condition. Now it gets only the rooms that met the department condition

Upvotes: 2

Related Questions