Passing PHP Variable from GET to current form if isset laravel 5

Ad

Ok, I've searched the last couple of days and haven't been able to find an answer that made sense. I'm positive it's doable but not sure if I'm in over my head. I'm new to Laravel 5 and also pretty new to PHP. I'm trying to send a inputted string from the GET['quote'] part of my (laravel collective) form to the same form that is updated with a new view below it, that utilizes the GET variable. Basically keeping whatever someone types in the searchbox after refresh.

Normally I would just echo out the needed variable if it's "set" then leave it empty if it isn't in the 'value' = "" section of the textbox. In Laravel with this form I'm not able to do that due to the rendering of the child view in the parent view(parent first, then child), if I understand correctly. Is there any work around? I'm about ready to just do the old echo within the form if I can't find a solution.

Thanks in advance! This site has helped me with a lot of my questions over the last few months.

Current Form on a Parent View.

{{ Form::open(array('url' => 'search', 'method' => 'get')) }}
{!! Form::text('query', '', [
                    'class' => "form-table",
                    'placeholder' => "Search Keywords",
                    'value' => "{{ $query }}"
                    ]) !!}</td>
{!! Form::submit('Submit') !!}
{{  Form::close() }}

Code below form is to pass the textbox string to another view and show it below this form. I don't know any JS so I'm kind of fumbling about here probably trying to make something work that just wont work.

@yield('child')

Code below here is from my controller.

public function getSearch(){
$title = "Page Title";
$query = "some string"; //This was set to $_GET['query'] but was failing.
return view('pages.search')->with("title", $title)->with("query", $query);

Working Code: For anyone that runs into this issue


Form Code: value = "" was in the wrong place on my original form as well so I've placed the $query into the proper form array below.

{{ Form::open(array('url' => 'search', 'method' => 'get')) }}
{{ Form::text('query', $query, [
                    'class' => "form-table",
                    'placeholder' => "Search Keywords",
                    ]) 
}}
{{ Form::submit('Submit') }}
{{  Form::close() }}

Pages Controller. Added to the top of my controller below namespace.

use Illuminate\Http\Request;

Controller:

public function getSearch(Request $request){
$title = "Search";
$query = $request->input("query", "");
return view('pages.search', ["title" => $title, "query" => $query]);
}
Ad

Answer

Ad

You were close, but missing a couple of key parts. First you need to pass in the Request to the controller to access, well, the request.

I would highly recommend reading up on this part of the docs before you get stuck into Laravel too much. Responses and requests are the heart of all things back end:

https://laravel.com/docs/5.1/requestshttps://laravel.com/docs/5.1/responses

Controller

use Illuminate\Http\Request;

public function getSearch(Request $request)
{
    $title = "Page Title";
    $query = $request->input("query", ""); // get the 'query' string, or default to an empty string

    return view('pages.search', [
        'title' => $title,
        'query' => $query,
    ]);
}

View

{!! Form::open(['url' => 'search', 'method' => 'get']); !!}
{!! Form::text('query', $query, [
                    'class' => "form-table",
                    'placeholder' => "Search Keywords"
                    ]); 
!!}
{!! Form::submit('Submit'); !!}
{!! Form::close(); !!}
Ad
source: stackoverflow.com
Ad