I'm building a website with Laravel and I want to create a search like a form where a user can search movies using different genres. The problem is when I try to use more than 1 genre it only displays the last option selected for each of my fields. For example, my search options are genre and year and I try to input comedy, romance, action, 1992, 2005 but only outputs action movies in 2005.
All the data is stored in a MySQL database and I used a basic to do the input with checkboxes, the data appears in the URL and does the search but only works for one of the options on each field.
<form action="/home" method="GET">
<hr>
<h4>Genre</h4><br>
@foreach($movies as $movie)
<input type="checkbox" name="genre" value="{{$movie->genre}}">{{$movie->genre}}<br>
@endforeach
<hr>
<h4>Year</h4>
@foreach($movies as $movie)
<input type="checkbox" name="year" value="{{$movie->year}}">{{$movie->year}}<br>
@endforeach
<input type="submit" class="btn btn-success" value="Search"><br>
</form>
The idea is that if a user inputs comedy, romance, action, 1992, 2005 the output show comedy movies, action movies and romance movies in 1992 and 2005.
Edit
Here is my controller where I do the search and a pagination.
public function index(Request $request)
{
$movies = \DB::table('movies');
$queries = [];
$columns = [
'genre','year'
];
foreach ($columns as $column) {
if (request()->has($column)) {
$movies = $movies->where($column, request($column));
$queries[$column] = request($column);
}
}
$movies = $movies->orderBy('created_at', 'desc')->paginate(20)->appends($queries);
return view('home', compact('movies'));
}