Ad

Laravel: Data To Be Displayed On Page Using Drop Down List

- 1 answer

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 ?

The output of this is currently showing the hotel id in the URL E.G (http...search?title=3) showing that it is partly working, however i do i manage to display it on my page ?

Search.blade.php

@extends ('layouts.app')
@section('content')  

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

                <p>{{ $request->title }}</p>
                <p>{{ $request->distance }}</p>
                <img src="{{$request->image}}" height = 200 width =200>

    @endif


SearchController.php


   public function index(Request $request)
   {
       $posts = Post::all();

       $selectedPost = $request->has('distance')
           ? Post::find($request->distance)
           : null;

       return view('Pages.search', [
           'posts' => $posts,
           'request' => $selectedPost
       ]);
   }

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


Ad

Answer

Logic to retrieve data should be placed inside the controller, the view only shows the data.

search.blade.php

{!! Form::open(['action' => '[email protected]', 'method' => 'GET']) !!}
<div class="form-group">
    <select name="post_id" 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>

@if($post)
    <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 

App/Http/Controller/SearchController.php

namespace App\Http\Controllers;

use App\Post;
use Illuminate\Http\Request;

class SearchController extends Controller {

    public function index(Request $request)
    {
        $posts = Post::all();

        $selectedPost = $request->has('post_id')
            ? Post::find($request->post_id)
            : null;

        return view('Pages.search', [
            'posts' => $posts,
            'post' => $selectedPost
        ]);
    }

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

Ad
source: stackoverflow.com
Ad