Making search and utilizing URL

Published 1 month ago by antonija

Hi everybody. I am still learning and I have some code for search results that works but I am not happy with it. I want to have something like this https://laracasts.com/search?q=laravel

This part ?q=laravel. How do I get this URL and results according to it?

tisuchi
tisuchi
1 month ago (262,165 XP)

https://laracasts.com/search?q=laravel

In your search form-

<form action="https://laracasts.com/search" method="POST">
    <input type="text" name="q" />
    <input type="submit" value="search" />
</form>

In your route-

Route::get('search', '[email protected]');

In your controller-

public function showSearch(Request $r){
    $q = $r->input('q');

    $data = Model::where('someColumn', 'LIKE', "%$q%")->get();

    return $data;
}

antonija

Ok, I can read and hopefully understand this code. Mine is very similar, but on next page, I see route which goes https://laracasts.com/search, this part never shows ?q=laravel. What about that?

I need this because I want to be able to copy URL and give it to a friend. When they open it they should see results I have seen.

tisuchi
tisuchi
1 month ago (262,165 XP)

If you mean pagination with search url, here is the example-

return view('foldername/filename', [
    'data' => $data->appends($r->input('q'))
]);

Read more: http://tisuchi.com/Laravel/laravel-pagination-with-url-parameters

antonija

I'm getting confused.

www.classifieds.com/search?type=cars+color=blue

I am sure I wrote this in a wrong way but you get the point, right? If I were to send this link to you, then you should see results I previously saw.

tisuchi
tisuchi
1 month ago (262,165 XP)

Let me clear first that whether I can understand you properly or not.

Is that something like this that, you see one result, and I get different result from same link?

www.classifieds.com/search?type=cars+color=blue

If not, you better show us an example.

antonija

No. When you run this link in a browser you should see data that conforms to a type of car and to a color of blue, that way you don't have to use search form yourself to get data.

tisuchi
tisuchi
1 month ago (262,165 XP)

I see.. Let me start from sketch.

Imagine that, you have two field in your form called-

  • types of car
  • color of car

In html form code, it would be looks like that-

<form action="https://laracasts.com/search" method="POST">
    <input type="text" name="type" />
    <input type="text" name="color" />
    <input type="submit" value="search" />
</form>

Now, if you submit this form, in url you will get something like this-

www.classifieds.com/search?type=some&color=blue

Notice that, two variables type and color were separated by &. This is how you need to pass data into url.

Note: you can eliminate form also, if you want.

Access via url.

Imagine that, you have passed your url like this

www.classifieds.com/search?type=some&color=blue

Now in your controller, you need to receive your value like this way-

public function showSearch(Request $r){
    $type = $r->input('type');
    $color = $r->input('color');

    //do whatever you want now.
}
antonija

Ok, this seems like something I need to do. How do I specify the route in this case?

tisuchi
tisuchi
1 month ago (262,165 XP)

Just simply create a get route. Inside your controllers method, just check whether it has value or not.

I highly recommend for your to watch and practice these two series that will increase your understanding about laravel lot.

antonija

I have watched most of the first link... I will definitely check the second one as well.

Is it something like this, am I close?

Route::get('/search?type={type}&color={color}', '[email protected]');

tisuchi
tisuchi
1 month ago (262,165 XP)

No.

Just use-

Route::get('search', '[email protected]');

In all the get url, you can pass any amount of parameters. For example, in above url, you can pass data like this way-

www.example.com/search?type=some&color=blue
Kaori

Hi tisuchi, I'm new of Laravel too. Can I ask how you past parameter but don't put them in form to submit? .Can you make simple example for us? At view - at controller and model. Thanks

Please sign in or create an account to participate in this conversation.