TheCodingKid
TheCodingKid

Reputation: 115

laravel: display data from a drop down list

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.

Upvotes: 2

Views: 202

Answers (3)

Sjoerd Loeve
Sjoerd Loeve

Reputation: 231

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

Upvotes: 1

Azeame
Azeame

Reputation: 2401

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.

Upvotes: 0

Ripon Uddin
Ripon Uddin

Reputation: 714

Change this at Controller :

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

Try this. Hopefully work for you

Upvotes: 0

Related Questions