I'm using Laravel to make a bulletin board, and I want the articles on the bulletin board to be displayed by selecting a year (for example, when 2022 is selected, only articles published in 2022 will be displayed). I referred to a lot of similar questions and Laravel's official website but it didn't go well, this is what I got now:
Select "blade.php":
<select name="article_year" onchange=""> @foreach ($year as $item) <option value="{{ $item->id}}">{{ $item->name}}</option> @endforeach </select>
index() in "Controller.php":
$article_year = $request->get('article_year'); $data['news'] = (new Article())->where('user_year', $article_year)->get();
Can anyone tell me where the problem is? Thanks!
Some websites I referenced:
Laravel - HTTP Request
Laravel Discussion - How to get value from selection box
Pass selected value from view to controller in Laravel
Complete code of the controller
<?php namespace App\Http\Controllers; use App\Article; use App\Catalog; use App\User; use App\Http\Controllers\Controller; use Request; use App; class HomeController extends Controller { public function __invoke(){} public function index() { $locale = Request::segment(1); $view = 'home'; $article_year = $request->get('article_year'); $data['year'] = (new Catalog())->where('catalog_type_id', 3)->where('is_active', 1)->orderBy('sort_num', 'asc')->get(); $data['news_pin'] = (new Article())->where('is_active', 1)->where('user_year', $article_year)->orderBy('sort_num', 'desc')->get(); $data['news'] = (new Article())->where('is_active', 0)->where('user_year', $article_year)->orderBy('sort_num', 'desc')->get(); return view($view, $data); } }
Thank you everyone for your answers, everyone gave me different ideas and helped me find the answer. My code is running:
routing-web.php
blade.php
Controller
I think you're missing some dependencies passing the index method... that's why you're getting the 500 server error
In your index method, you are not passing $request variable..I think the code should be like this
And add this line to the top of the controller file