How to save database details using Laravel Route as API in Ionic

Data manipulation often requires a middleware we mostly call ‘An API’ to send and receive data between a client-server application. A web API mostly has multiple SQL queries that connect a clients apps to the database server.

Modern frameworks and technologies like Laravel has made the process pretty simple and easy. We have techniques using Laravel that make us create routes in the project that create or update user data and update or create a user record. 

Suppose you have a Notification table in your database. The Laravel application handling your database functions handles all calls to your database.

Laravel Route as API URL

To make Laravel routes work as an API to save database details, you can follow these steps:

  1. Define the route: In your Laravel routes file (usually routes/web.php or routes/api.php), define a route to handle the API request. For example, to handle a POST request to save database details, you can define a route like this: Route::post('/save-details', 'App\Http\Controllers\DetailsController@save');
  2. Add updateOrCreate function adding the fields database in it.

UpdateOrCreate function checks against the device token variable, if the database table has an existing entry of the device token, it updates the details entered for other fields. Otherwise, a new record with the device token you enter is created. This route url makes you add a database record by just passing the URL in your browser. 

Consider the URL along with the parameters as shown below. Enter the URL in your browser along with the values and this adds or updates a record to your database.

https://scratchcoding.dev/save-data/451245/1/0572365441

Simple as that! Now use the url along with your desired input values for each field and get them saved in the database directly. 

We can use this URL in any client based application. Consider the example discussed in the previous tutorial to send data to database server from an Ionic App using an API service. We can use this url along with the parameters in the http.post method. 

Leave a Comment