Home > Net >  Laravel - Controller get select value from Views
Laravel - Controller get select value from Views

Time:05-10

I'm making a bulletin board using Laravel, and I want the articles on the board can shown by selecting the years (ex. when choosing 2022, only show the articles post in 2022). I refer to many similar questions and Laravel's offical website but not went well, there is what I got right now:

select in ' 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 any one tell me what the problem is? Thanks!

Some website I refer to:

Laravel - HTTP Requests

Laravel Discuss - How to get value from Select box

Passing the value of a select from view to controller in Laravel

full code of 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);
    }
}

CodePudding user response:

Thanks for all answering, every one give me the different thought and help me found the answer. There is my code which is working:


route-web.php

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

blade.php

<form>
    <select name="article_year">
        @foreach ($year as $item)
            <option value="{{ $item->name}}">{{ $item->name}}</option>
        @endforeach
    </select>
    <input type="submit" value="Submit Form" />
    </form>

Controller

<?php

namespace App\Http\Controllers;

use App\Article;
use App\Catalog;
use App\User;
use App\Http\Controllers\Controller;
use App;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function index(Request $request)
    {
        $view = 'home';
        $value = $request->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);
    }
}

CodePudding user response:

I think you have missed some depencies to pass on index method.... That the reason you getting 500 server error

In your index method you have not passing the $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 also add this line on top of the conrtoller file

use Illuminate\Http\Request;

CodePudding user response:

Try this

$article_year = $request->get('article_year');
$data['news'] = Article::where('user_year', $article_year)->get();

return $data;
  • Related