1

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]);
}
Lost Soul
  • 15
  • 4
  • You don't need the blade tokens in the value `value` (`{{` & `}}`). If it truely is in the `GET` you should be passing in the `Request` object to the controller and accessing it that way – Chris Dec 26 '15 at 07:40
  • I did a quick search of that and will continue to look for some clarification as it's beginning to make sense, but I'm not completely there. What if the object is empty, ie first time landing on that page? – Lost Soul Dec 26 '15 at 08:07

2 Answers2

1

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/requests https://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(); !!}
Chris
  • 54,599
  • 30
  • 149
  • 186
  • That's a huge help. I think the biggest thing to understanding the responses and requests are having a somewhat real world application on how to implement them. I read both of those pages a couple times today and yesterday but couldn't make sense of it. With your modifications that helps a ton in understanding it. I'll be working on this again later today and get back to you on how it all pans out. Thanks a ton. – Lost Soul Dec 26 '15 at 09:53
  • No worries. Please remember to mark correct if it is indeed the solution to your problem. It helps not only you, but future stackoverflow users – Chris Dec 26 '15 at 10:15
  • For some reason I'm getting the same error I got messing around with requests. It's stating the: request variable is undefined. – Lost Soul Dec 26 '15 at 22:20
  • Ok, I was missing. 'use Illuminate\Http\Request;' in my Page Controller. I'm still not getting the old values posted to my form though. I'm not sure why... Might have to do with the Laravel Collective not recognizing the variable. I can echo it out under the form above the next view but I still have an empty value = "" in my source.... odd. – Lost Soul Dec 26 '15 at 22:49
  • It seems the second argument of the collective text method should be the default value, I have updated the answer to reflect that – Chris Dec 27 '15 at 00:49
0

First check if query has any value. If not it will return an empty string as the second argument of input field is set to ''. Then it will search through the database for the given keyword.

Controller:

public function getSearch(Request $request)
{
    $title = "Page Title";
    if(Input::has('query')){
        $query = Input::get('query');
    }
    $data = DB::table('table_name')->where('column_name','like','%'.$query.'%');

    // or if you use model you can use this
    $data = ModalName::all()->where('column_name','like','%'.$query.'%');

    return view('pages.search', compact('title','data'));
}

Blade:

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

// To view data add the table below search form

<table>
    <tr>
        <th>col1</th>
        <th>col2</th>
        <th>col3</th>
    </tr>
    @foreach($data as $d)
    <tr>
        <td>{{ $d->column_one }}</td>
        <td>{{ $d->column_two }}</td>
        <td>{{ $d->column_three }}</td>
    </tr>
    @endforeach
</table>
smartrahat
  • 5,381
  • 6
  • 47
  • 68
  • I appreciate the response but this is just a simple form passing a simple variable to another view. Your response is spot on though if I was trying to access a DB, then loop out the contents based on a query. Although the name of my text input is query its actually not a query. – Lost Soul Dec 26 '15 at 23:16