search

Home  >  Q&A  >  body text

Laravel - Controller getting selection value from view

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粉729436537P粉729436537231 days ago3676

reply all(2)I'll reply

  • P粉253800312

    P粉2538003122024-03-27 22:40:26

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

    reply
    0
  • P粉464208937

    P粉4642089372024-03-27 14:54:15

    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;

    reply
    0
  • Cancelreply