php - query string with laravel


i have a problem with a query string in Laravel 5.6

I have this two entities, ER Schema: ER Schema

and related model: Note model Location model

My problem is that I have to select all notes in a certain location passing as query string parameters latitude and longitude.

A query string could be:


I try to build a query Here the NoteController code, but there is a problem:

Actually the output show me every note. This is a postman output and in addition to the notes in the right location, I also return those with location = null. Going to see in the database these notes have only one parameter that coincides with those passed in query string. Theoretically it should not return them and I can not take them off in any way. could someone help me?

I hope I was clear enough, thanks in advance




Node::with(['location' => function($query) use ($latitude, $longitude){
    $query->where('latitude', $latitude);
    $query->where('longitude', $longitude);



If it can help someone, I solved the problem in this way.

public function index(Request $request) {

    if($request->has('lat') &&
        $request->has('long')) {

        $lat1 = $request->query('lat');
        $long1 = $request->query('long');
        $max_distance = $request->query('max_distance');

        $notes = Note::All();

        foreach ($notes as $key => $value) {

            if($value->location != null) {
                $lat2 = $value->location->latitude;
                $long2 = $value->location->longitude;
                $distance = sqrt(pow($lat2-$lat1, 2) + pow($long2-$long1, 2));

                if($distance > $max_distance) {
            } else {


        return $notes;
    return Note::All();

People are also looking for solutions to the problem: php - Insert into table doesn't go in Postgres


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.