Support the ongoing development of Laravel.io →
Eloquent Blade Forms
Last updated 2 years ago.
0

Changing my first reply, hehe...

<form action="/homes" method="post">
    <input type="text" name="search" />
    <button type="submit" name="type" value="latest">Button 1</button>
    <button type="submit" name="type" value="popular">Button 2</button>
</form>

Then you build your query conditionally based on the type. The query builder is great for this kind of work. Consider also storing your Eloquent statement to a variable first then passing in to View::make That will be cleaner.

Last updated 2 years ago.
0

This might sound silly but here's how I'm doing it now. I got it to work but i know it' not the "correct way". Please guide me to the more elegant way of doing this.

So In my View I created links that point to individual actions within my controller and when these links are clicked should filter the results for "oldest" "newest" and "most reviews" like so:

{{ HTML::linkAction('ListingsController@getSearchfilteroldest', 'Oldest') }}
{{ HTML::linkAction('ListingsController@getSearchfilternewest', 'Oldest') }}
{{ HTML::linkAction('ListingsController@getSearchfiltermostreviews', 'Oldest') }}

So the functionality of these actions are the same as my normal search, but it i added the orderBy('created_at', 'DESC') and the same for ASC which obviously filters for oldest and newest and then for most reviews i did a ->where to get the highest values in the column.

So what I did to "remember" what the user searched for initially is to store the inputs in the session like so:

Session::put('keyword', $keyword);
Session::put('city', $city);

and then I called those variables back in the actions i just created. Then in the view I just used the same @foreach loop to display the new filtered results.

So basically I just created different actions for each filter and linked to them, and all the actions do the same query as the initial search. I "remembered" what was searched for by "putting" the Input values in the Session, and the filtering actions are the same as the action for the initial search but has just the additional piece of query to do the relevant filtering of the results

Hope this is understandable!

Thanks for the Help again in advance!!

Last updated 2 years ago.
0

You can post it all to a single route, look again at the code I posted for you. You can know what button they clicked from the submit value.


$homes = Homes::take(20);

if (Input::get('type') == 'popular')
{
    $homes->orderBy('popular');
}
elseif (Input::get('type') == 'latest')
{
    $homes->orderBy('created_at');
}

$homes = $homes->get();

It's not exactly the code you want but you can get the gist. From the form I posted you will have all the information you need: the query and the search type.

Last updated 2 years ago.
0

I'd recommend using get variables, and setting yourself up some scopes, to make things easy.

public function scopePopular($query)
{
    return $query->orderBy('populatr');
}

public function scopeLatest($query)
{
    return $query->orderBy('created_at', 'desc');
}

Then you can either limit to one type per request like above, or allow multiple.

Limited:

$homes = Homes::take(20);
if(Input::has('type')) {
    switch(Input::get('type')) {
        case 'popular': $homes->popular(); break;
        case 'latest': $homes->latest(); break;
    }
}

Multiple:

$homes = Homes::take(20);
if(Input::has('popular')) {
    $homes->popular();
}
if(Input::has('latest')) {
    $homes->latest();
}

:)

Last updated 2 years ago.
0

Good suggestion Ollieread thanks for pointing that out.

There's just still only 1 thing that I need help on... or maybe I did it right... Is using Session::Put and Session::Get the most elegant way to "remember" the initial search (before filtering)? Heres what I have now:

I created a scope as Ollieread suggested like below (You will see that i used the Session to get back the keyword and the city that the user searched for,

public function scopeOriginalSearch($query){
	$keyword = Session::get('keyword');
	$city = Session::get('city');

	return $query->where('title', 'LIKE', '%'.$keyword.'%')->where('location_id','=', $city);
}

Then my controller action to filter the results is as follows:

public function getSearchfilter(){

		$filteredFreelancers = Freelancer::take(20);
		$filteredFreelancers->OriginalSearch();

		if (Input::get('type') == 'newest')
		{
		    $filteredHouses->orderBy('created_at', 'DESC');
		}
		elseif (Input::get('type') == 'oldest')
		{
		    $filteredFreelancers->orderBy('created_at', 'ASC');
		}

		$filteredHouses = $filteredhouses->get();


		return view::make('listings.searchfilter')
			->with('houses', $filteredHouses) ;
}
Last updated 2 years ago.
0

Can anyone help with my last question? I'm pretty sure it's very simple... Newbie question really.

Last updated 2 years ago.
0

Hi anthonycha, I have exact the same issue in my development, I see you have posted this like 5 months ago, so you should already solved this. Can you please let me know how have you solved this?

Thanks a lot for your time and help.

Regards,

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

anthonycha anthonycha Joined 22 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.