Home > Net >  Laravel Controller returning wrong view
Laravel Controller returning wrong view

Time:11-08

Why is Laravel Controller returning the "index" view instead of the "show" view when the form is submitted?

I'm using Laravel 9.38.0

HTML

<form action="jobs" id="form-job-search" method="GET">
  @csrf
  <input type="text" name="what" placeholder="Job" required>
  <input type="text" name="where" placeholder="City" required>
  <input type="submit" id="button-job-search" value="Find Jobs">
</form>

Route

Route::get('/jobs', [JobController::class, 'index']);
Route::get('/jobs/{what}/{where}', [JobController::class, 'show']);

Controller

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class JobController extends Controller
{
  public function index() {
    return view('jobs.index');
  }

  public function show($what, $where) {
    return view('jobs.show');
  }
}

CodePudding user response:

Please try this Form

<form action="{{url('jobs')}}" id="form-job-search" method="post">

// here form content

Route change to from

Route::get('/jobs/{what}/{where}', [JobController::class, 'show']);

to

Route::post('jobs', [JobController::class, 'show']);

Controller

 public function show(Request $request) {
    $what=$request->what;
    $where=$request->where;
    return view('jobs.show');
  }
  • Related