Sirius
Sirius

Reputation: 653

Get all the cities of a state

I would like to list all the cities of a state.

Here an example of my table "properties"

+----+-------------+--------+--------+---------------+
| id | title       |price   | state  | city          |
+----+-------------+--------+--------+---------------+
| 1  | test        | 55     | Florida| Jacksonville  |
+----+-------------+--------+--------+---------------+
| 2  | test 2      | 30     | Nevada | Las Vegas     |
+----+-------------+--------+--------+---------------+
| 3  | test 3      | 200    | Florida| Miami         |
+----+-------------+--------+--------+---------------+

index.blage.php template:

    @foreach($all_cities as $city)
    <div class="col-lg-2">
        <a href="{{url('listing?keywords=' . $city->city )}}">  {{$city->city}}</a>
    </div>

    @endforeach

HomeController.php

   public function home()
    {

        $all_cities = Property::select("city")->groupBy('city')->get();
        return view('index', ['all_cities' => $all_cities]);

    }

For example get all cities of Florida.

Any help would be appreciated

Thank you

Upvotes: 1

Views: 78

Answers (2)

MrZzippo p
MrZzippo p

Reputation: 24

This would do the trick

$all_cities = Property::select("city")->where("state","=","Florida")->get();

Upvotes: 0

Maraboc
Maraboc

Reputation: 11083

Try it like this :

$cities = Property::select("city")
                        ->whereState('Florida')
                        ->get();

Upvotes: 3

Related Questions