2

I am implementing a drop down menu list to display a list of hotels from my system. I currently have the following code which when i click submit, nothing is happening (no data displayed).

Does anyone know how i can potentially display data once i press submit ?

Search.Blade.php

{!! Form::open(['action' => 'SearchController@index', 'method' => 'POST']) !!}

 <div class="form-group">

 <select name="title" id="title" class="form-control input-lg dynamic" data-dependent="state">

 @foreach($posts as $post)
    <option value="{{$post->id}}">{{$post->title}} </option>
 @endforeach

 </select>
 <br>
  <div class="form-group">
 {{Form::Submit('submit', ['class' => 'btn btn-primary'])}}
 </div>


    <div class="table">
    <table>
    <tr>
     <th>Hotel Name</th>
     <td>{{$post->title}}</td>
     <th>Distance</th>
     <td>{{$post->distance}}</td>
     <th>Images</th>
     <td>{{$post->image}}</td>
    </tr>
  <tr>
  </tr>
 </td>
    </div> 

SearchController.php

  public function index()
    {
     $posts = Post::get();
     return view('Pages.search', compact('posts'));
     }
   public function store(Request $request)
  {



  }

So when i press submit, it takes me to a white blank page. Im not sure how to retrieve the data from the selected drop down list and display it on my page, if any one knows how to do so please advise.

1
  • This is the very basics of Laravel, you should follow a tutorial or read the online documentation: laravel.com/docs/6.x Commented Feb 20, 2020 at 15:49

3 Answers 3

1

After submitting the form you will land on the store() page because you are firing a POST request. Your store method is empty so you will get an empty screen. Make your store method like this to start with:

public function store(Request $request)
{
    // This will return all request data to your screen.
    return $request->all();
}

If you would like to stay on the index method you have to change your form method to GET. Then you only have to get the Post details from the template, since you're already sending all Posts from the index method:

{!! Form::open(['action' => 'SearchController@index', 'method' => 'GET']) !!}
<div class="form-group">
    <select name="title" id="title" class="form-control input-lg dynamic" data-dependent="state">
        <option value="">Choose an item</option>
        @foreach($posts as $post)
            <option value="{{ $post->id }}">{{ $post->title }}</option>
        @endforeach
    </select>
</div>
<div class="form-group">
    {{ Form::Submit('submit', ['class' => 'btn btn-primary']) }}
</div>
{!! Form::close() !!}

@if(request('title') and $item = $posts->where('id', request('title')))
    <div class="table">
        <table>
            <tr>
                <th>Hotel name</th>
                <td>{{ $post->title }}</td>
                <th>Distance</th>
                <td>{{ $post->distance }}</td>
                <th>Images</th>
                <td>{{ $post->image }}</td>
            </tr>
        </table>
    </div>
@endif
Sign up to request clarification or add additional context in comments.

8 Comments

Hi this did something, not quite there yet, it returned a white blank page with : {"_token":"dJBc0ak8fGN92MuOVebdi9bWQw76AukJfU0q5Fi4","title":"3"}. This is correct as its returning the correct value (3), however how do i get it to display on my page using the updated code on search.blade.php ?
Also is it worth changing 'post' to GET as i am getting the data
Yes, if you change POST to GET you will stay on the index method and can use $request->get('title') to get your request data here. Or in your template like {{ request('title'). Depending on you purposes..
sorry just got confused with the $request->get(title) stuff, can you maybe change the code a little to see how it should look. So that it will display on my page when i press submit.
so now i have noticed that in my url it says fyp.test/search?title=4 so i know that it is working, however now i need to know how to display this information onto my screen.
|
0

Change this at Controller :

Post::get(); to Post::all()

Try this. Hopefully work for you

Comments

0

If you are using Laravel's Resourceful Controller Routing then the index() route handles GET requests, and the store() route handles POST requests to the same endpoint.

Although you have your form action as SearchController@index it only resolves to a URL which happens to be the same URL as SearchController@store and what separates how Laravel handles them is the request method as I mentioned above.

Therefore you are probably looking for something along these lines:

public function store(Request $request) { 
    $post = Post:: findOrFail($request->title); 
    return view('Pages.[page-name-here'], compact('post')); 
}

If you have any trouble understanding which route responds to the combination of endpoint and method go to your command line and use php artisan route:list to see the endpoint mappings for your routes.


$request->title

As used above matches the <select name='title'> in your blade template. You may want to change them to post for when you come back to it down the road you can understand what's going on.


findOrFail()

As used above will throw a 404 not found error and stop execution if a matching Model isn't retrieved from the database with the given title. You could just as well use find() and check for a Post model and redirect back to the previous page, throw a validation failed or whatever your desired response is when a Post isn't retrieved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.