-1

I am trying to correct my code of the searchresult function of my controller so my concern is that when I paginate the search result or make form validation, I get the following error message : The GET method is not supported for this route. Supported methods: POST. so if you can help me, I want to change my code from returning the view to returning the route. here is my code

Route:

Route::get('/post/searchpost', 'PostsController@index')->name('index');
Route::post('/post/search_result', 'PostsController@searchresult')->name('searchresult');

Blades //index

<div class="posts">
<h1>Posts</h1>
<div class="sreach">
    <form action="{{route('searchresult', app()->getLocale())}}" method="POST">
      {{ csrf_field()}}
      <div class="form-row">
        <div class="form-group col-md-2">
          <label class="lbl" for="cat_id">title</label>
          <select class="form-control" id="cat_id" name="cat_id">
                  <option value="" selected></option>
          @foreach ($categoriesas $cat)
                  <option value="{{$cat->id}}">{{$cat->cat_mame}}</option>
          @endforeach
          </select>
        </div>

        <div class="form-group col-md-2">
          <label class="lbl" for="title">Post title</label>
          <input type="text" class="form-control" id="title" name="title">
        </div>
        <div class="form-group col-md-2">
          <label class="lbl" for="content">Post content</label>
          <textarea class="form-control" id="content" name="content">
          </textarea >
        </div>
      </div>
      <center>
        <button type="submit">search</button>
      </center>
    </form>
  </div>

  <table id="posts">
    <thead>
      <tr>
        <th>{{__('main.title')}}</th>
        <th>{{__('main.post_category')}}</th>
        <th>{{__('main.content')}}</th>
      </tr>
    </thead>
    <tbody>
          @foreach($posts as $postkey => $post)
              <tr>
                  <td>{{$post->title}}</td>
                  <td>{{$post->category->cat_name}}</td>
                  <td>{{$post->content}}</td>
              </tr>
          @endforeach
    </tbody>
  </table>

Blades //searchresult

    <div class="posts">
        <h1>Posts</h1>
        <div class="sreach">
//that make me search again in result page using the same form
            <form action="{{route('searchresult', app()->getLocale())}}" method="POST">
              {{ csrf_field()}}
              <div class="form-row">
                <div class="form-group col-md-2">
                  <label class="lbl" for="cat_id">title</label>
                  <select class="form-control" id="cat_id" name="cat_id">
                          <option value="" selected></option>
                  @foreach ($categoriesas $cat)
                          <option value="{{$cat->id}}">{{$cat->cat_mame}}</option>
                  @endforeach
                  </select>
                </div>
    
                <div class="form-group col-md-2">
                  <label class="lbl" for="title">Post title</label>
                  <input type="text" class="form-control" id="title" name="title">
                </div>
                <div class="form-group col-md-2">
                  <label class="lbl" for="content">Post content</label>
                  <textarea class="form-control" id="content" name="content">
                  </textarea >
                </div>
              </div>
              <center>
                <button type="submit">search</button>
              </center>
            </form>
          </div>
    
          <table id="posts">
            <thead>
              <tr>
                <th>{{__('main.title')}}</th>
                <th>{{__('main.post_category')}}</th>
                <th>{{__('main.content')}}</th>
              </tr>
            </thead>
            <tbody>
                  @foreach($details as $detailkey => $detail)
                      <tr>
                          <td>{{$detail->title}}</td>
                          <td>{{$detail->category->cat_name}}</td>
                          <td>{{$detail->content}}</td>
                      </tr>
                  @endforeach
            </tbody>
          </table>
    </div>

Controller

public function index()
    {
        $ctegories=  Category::all();
        return view('search')->with('posts', Post::all())->with('ctegories', $ctegories);
    }

    public function searchresult(Request $request)
    {
        $title= $request->title;
        $content= $request->content;
        $cat_id= $request->cat_id;

        $posts= Post::where('title', 'like', '%' . $title. '%')
        ->whereHas('category', function(Builder $query) use ($sujet_id){
            $query->where('id', $cat_id);
        })
        ->orderBy('created_at','desc')
        ->get();
        //for form dropdown categories
        $categories=  Category::all();

        if(count ($posts) > 0)
        {
            //this what i want to change from view to route with those parameters
            return view('searchresult')
            ->withDetails($posts)->withQuery($title)
            ->withCategories($categories);
        } else {
            return redirect()->route('index')->with('success','search not found');
        }
    }

So otherwise the function works perfectly and I get the data i will appreciate your help thank you

CodingStudent
  • 53
  • 1
  • 1
  • 9

1 Answers1

0

You should use below method to return route with parameters:

return \Redirect::route('nameOfRoute', ['param1'=>'value1','param2'=>'value2',...])->with('message', 'Search found!!!');
Amit Senjaliya
  • 2,867
  • 1
  • 10
  • 24