Laravel - Controller getting selection value from view
P粉729436537
P粉729436537 2024-03-26 23:25:12
0
2
3664

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);
    }
}

P粉729436537
P粉729436537

reply all(2)
P粉253800312

Thank you everyone for your answers, everyone gave me different ideas and helped me find the answer. My code is running:


routing-web.php

Route::get('/', 'HomeController@index')->name('home');
Route::post('/', 'HomeController@index');

blade.php


Controller

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', $value)->orderBy('sort_num', 'desc')->get();
        $data['news'] = (new Article())->where('is_active', 0)->where('user_year', $value)->orderBy('sort_num', 'desc')->get();

        return view($view, $data);
    }
}
P粉464208937

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

public function index(Request $request)
    {
        $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);
    }

And add this line to the top of the controller file

use Illuminate\Http\Request;
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!