mercredi 4 septembre 2019

How to get city id with city name from google maps in laravel api

I need help with following

I have a tickets logging system which has these three tables amongst other cities, companies, and tickets. So now I am using tickets table to log tickets via API. The tickets table has city_id as ref to cities and company_id as well which ref companies.

When user logs a ticket we use google maps to get user location, GPS coords and city name.

When use is submitting a ticket I want to search the city name selected by the user if exists in the cities table then get the id of that city + company_id associated with it.

then return the data and add it with the ticket data submitted by the user. I want to save the city_id not the city name from the google maps.

I can get the map to show me the users coords and city. I want to search laravel on submission for the city name on cities table then get id of the city and company_id.

Return those and add them as city_id and company_id to the tickets table.

  • ==== Ionic App get address code ==== *
        this.geoCoder.geocode({ location: { lat: latitude, lng: longitude } }, (results, status) => {
            console.log(results);
            console.log(status);
            if (status === 'OK') {
                if (results[0]) {
                    this.zoom = 12;
                    this.address = results[0].formatted_address;
                    for (var ac = 0; ac < results[0].address_components.length; ac++) {
                        var component = results[0].address_components[ac];

                        switch (component.types[0]) {
                            case 'locality':
                                this.city = component.long_name;
                                break;
                        }
                    }
                } else {
                    window.alert('No results found');
                }
            } else {
                window.alert('Geocoder failed due to: ' + status);
            }
        });
    }

=== Laravel api controller method ===

public function submitTicket(TicketRequest $request, Ticket $ticket)
    {
        $company = City::query();

        //Search for city_id using city return from the google maps
        if ($request->has('city')) {
            $company = $company->where('city', 'LIKE', '%' . $request->get('city') . '%');

        }

        $ticket = new Ticket;

        $ticket->ref_no = $this->getRefNumber();
        $ticket->service_id = $request->get('service_id');
        $ticket->ticket_subject_id = $request->get('ticket_subject_id');
        $ticket->description = $request->get('description');
        $ticket->user_id = $request->get('user_id');

        // Company id must be filled automatically from the cities table once search is done
        if(){ //if statement to check if city name matches on cities table then return company_id
        $ticket->company_id = $request->get('company_id');
        }

        $ticket->full_name = $request->get('full_name');
        $ticket->phone = $request->get('phone');
        $ticket->address = $request->get('address');

        // city id must be filled automatically from the cities table once search is done
        if(){ //if statement to check if city name matches on cities table then return city id
        $ticket->city_id = $request->get('city_id');
        }

        if ($request->file('photo')) {
            $ticket->photo = $this->upload($request->file('photo'), 'tickets')->getFileName();
        } else {
            $ticket->photo = $ticket->photo;
        }
        $ticket->latitude = $request->get('latitude');
        $ticket->longitude = $request->get('longitude');

        $ticket->save();

        if ($ticket) {
            $ticket->ref_no = ticketsNotification::ticketSubmitted($ticket->phone, $ticket->ref_no);
            $ticket->notify(new ticketCreatedNotification($ticket, $ticket->user->email));
        }

        return response()->json(['message' => 'ticket Submitted Succesffully'], 200);
    }


I want to get company_id and id from cities table which I will use to populate city_id and company_id in the tickets table.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire