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

I've made a small amount of progress since yesterday, but it still doesnt work completely like I want it to.

The below code works when the user searches by zip code (id), but I want them to be able to search by name instead.

How do I pass in the name of the city and get the listings based on that?

public function search()
	{

		$keyword = Input::get('keyword');

		$listings = Listing::where('city_id', '=', $keyword)->paginate(10);

		return View::make('listings.listings-search-results')
			->with('listings', City::where('id', 'LIKE', '%'.$keyword.'%')
			->get())
			->with('listings', $listings)
			->with('keyword', $keyword);
	}
Last updated 2 years ago.
0

I'm not sure I have clear understanding of your database schema I assume you have Listing table and then City table. so you want to get the listing based on the city name search.

This has two parts, normally you should get city_id from UI (probably by city dropdown), - this is just an advise because I don't think it is good idea to search by city name, cities are lookup collection and from that UI should pass city_id

anyway to build the relationship you can do following (hope you get the idea, but don't copy exactly as I don't know about your db schema)

in Listing model add following function

class Listing extends Eloquent {

    public function city()
    {
        return $this->belongsTo('City');
    }

}

in City model

class City extends Eloquent {

   public function listings()
   {
      return $this->hasMany('Listing')
   }
}

then

$listings = City::find($id)->listings;

// or 

$listings = City::where('city_name','like', '%'.$cityName. '%')->listings;

refer Laravel Eloquent ORM

Last updated 2 years ago.
0

Hey Technet! Thanks for answering my question :).

The problem is, that the end user have to search by the city name and not the ID (zip). I already got it working by using the ID, but obviously that is not what I really want.

Can I somehow reference the name according to the ID or?

Last updated 2 years ago.
0

Okay, I decided to do as you suggested, and make the UI control the search functionality so to speak. So the user chooses from a list, and then the ID is used to make the search.

Thank you for pointing me in the right direction! :)

Last updated 2 years ago.
0

Glad it helped, I'm not saying you shouldn't use City Name directly what I'm saying is it's two step process, say you have simple text box where user can type city name, and by ajax (there are lot of ajax controls you can use ex: select2 ) what it does is go and search matching city list, and finally user selects one out of them, then you have the id for that city which you can pass to find the listings,

however in above example I simply show you, if you need you can use city name in where clause (LIKE '%cityName%')

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Reached reached Joined 27 Feb 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.