Reputation: 3014
I have a Laravel PHP application up and running fine where all of my results are created, edited and stored just fine. Now I want to add a feature where there is a dropdown box on the homepage that allows the user to select a category and the homepage will display results depending on what the user selects from the dropdown box.
I have tried finding some documentation on how to accomplish this task but the only possible solution I found was by using AJAX with jQuery to update the results. I'm wondering if there is an easier, more elegant way of doing this.
View:
@section('content')
/* New section I recently added for including the dropdown box below */
<div class="row">
<div class="panel panel-default">
<div class="panel-heading clearfix">
<b>{{'Select a category' }}</b>
</div>
<div class="panel-body">
<dl class="dl-horizontal">
<div class="table-responsive">
<table class="table">
<tr>
<td>
{{Form::open(array('route' => 'select_vids_pics', 'files' => true)) }}
<div class="form-group">
/* Form Code goes here, 'label' and 'select options' */
<div class="form-group">
{{ Form::submit('Select Category', array('class' => 'btn btn-primary')) }}
</div>
{{ Form::close() }}
</div>
</td>
</tr>
</table>
</div>
</dl>
</div>
</div>
</div>
/* Blocks of code below for currently display all results */
<div class="row">
<div class="panel panel-default">
<div class="panel-heading clearfix">
<b>{{'Overview of Videos' }}</b>
</div>
<div class="panel-body">
@if ($allvids->count())
<dl class="dl-horizontal">
<!-- Add this div class to make the table responsive -->
<div class = "table-responsive">
<table class="table">
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Edge</th>
<th>Stone</th>
<th>Category</th>
<th>Project</th>
<th>Update Video</th>
</tr>
@foreach($allvids as $video)
<tr>
<td>{{ $video->video_id }}</b></td>
<td>{{ $video->video_name }}</td>
<td>{{ $video->video_description }}</td>
<td>{{ $video->video_edges }} </td>
<td>{{ $video->video_stones }}</td>
<td>{{ $video->category }}</td>
<td>{{ $video->video_project }}</td>
<td><b>{{ link_to_route("show_video", 'Edit', array($video->video_id)) }}</b></td>
</tr>
@endforeach
{{ $allvids-> links()}}
</table>
</div>
</dl>
@else
<b>{{ 'All Pics Vids' }}</b>
@endif
</div>
</div>
</div>
<div class="row">
<div class="panel panel-default">
<div class="panel-heading clearfix">
<b>{{'Overview of Pictures' }}</b>
</div>
<div class="panel-body">
@if ($allpics->count())
<dl class="dl-horizontal">
<!-- Add this div class to make the table responsive -->
<div class = "table-responsive">
<table class="table">
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Edge</th>
<th>Stone</th>
<th>Category</th>
<th>Project</th>
<th>Update Picture</th>
</tr>
@foreach($allpics as $picture)
<tr>
<td>{{ $picture->picture_id }}</b></td>
<td>{{ $picture->picture_name }}</td>
<td>{{ $picture->picture_description }}</td>
<td>{{ $picture->picture_edges }} </td>
<td>{{ $picture->picture_stones }}</td>
<td>{{ $picture->category }}</td>
<td>{{ $picture->picture_project }}</td>
<td><b>{{ link_to_route("show_picture", 'Edit', array($picture->picture_id)) }}</b></td>
</tr>
@endforeach
{{ $allpics-> links()}}
</table>
</div>
</dl>
@else
<b>{{ 'All Pics Vids' }}</b>
@endif
</div>
</div>
</div>
@stop
Controller:
/* This is the only function that is being used in the view above */
public function index()
{
$allvids = Video::paginate(10);
$allpics = Picture::paginate(10);
$this->layout->content = \View::make('home.pics_vids_overview', array('allvids' => $allvids, 'allpics' => $allpics));
}
Do I have to add another function/method to my controller for:
1) Accepting input from the dropdown box that the user selects 2) Passing that input to the newly defined function and then displaying the results based on that match?
If I have to use the approach above, would my function look something like this?
public function pics_vids_categories()
{
$input = \Input::all();
$videos = DB::table('videos')->where('category', '=', $input)->get();
$pictures = DB::table('pictures')->where('category', '=', $input)->get();
var_dump($videos);
var_dump($pictures);
}
Like I said, I'm hoping there's a prettier way of doing this.
Upvotes: 1
Views: 722
Reputation: 146239
You may change your index
method to something like this:
public function index()
{
$vdo = Video::query();
$pic = Picture::query();
if($category = Input::get('category')) {
$vdo->where('category', $category);
$pic->where('category', $category);
}
$allvids = $vdo->paginate(10);
$allpics = $oic->paginate(10);
$data = compact('allvids','allpics');
$this->layout->content = \View::make('home.pics_vids_overview', $data);
}
Now if the user selects a category and submits the form then where('category', $category)
will filter the result; otherwise all records/models will be returned.
Upvotes: 1