vendredi 24 novembre 2017

Laravel get database results from user's form input details

I've been searching for a solution to a query I'm trying to run. I have a database table called locations and I need the user to be able to submit a form to change/filter the results that are returned.

The 2 fields the user can select are charity & distance the fields aren't mandatory so it'll need to check if they are set.

I currently have it working with distance but can't get it to work when both are filled in using AND WHERE

here's my controller function

public function search(Request $request)
    {
        $circle_radius = 3959;
        $max_distance = $request->Input(['distance']);
        $charity = $request->Input(['charities']);
        $lat = '53.5526';
        $lng = '-1.479726';

        $locations = DB::select(
            'SELECT * FROM
                (SELECT *, (' . $circle_radius . ' * acos(cos(radians(' . $lat . ')) * cos(radians(latitude)) *
                cos(radians(longitude) - radians(' . $lng . ')) +
                sin(radians(' . $lat . ')) * sin(radians(latitude))))
                AS distance
                FROM locations) AS distances
            WHERE distance < ' . $max_distance . '
            AND charity = ' . $charity . '
            ORDER BY distance;
            ');

        return view('map.search', compact('locations'));

    }

If I take the AND charity line out it works with just distance but I need both



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire