Sneppy
Sneppy

Reputation: 387

Using AJAX with Laravel 5.5

I'm building a web application using Laravel 5.5

I created a basic user relationship system, and now I want to create a add friend button in the user profile. I don't want to wrap this inside a form just to send a friend request, so I was thinking to make a simple AJAX post request to the server passing the logged user id and the target user id:

$.post("/request/path", { "user_1": [...] }, function () {...});

However I'm new to Laravel (and routing in general) therefore I'm not sure if I should define a route in the api.php route file and make the request to, for example, /api/friend-request, or define a route in web.php like I did for login/register post requests. What's the proper way?

Upvotes: 0

Views: 1819

Answers (2)

Andry
Andry

Reputation: 67

You should put your route in /routes/web.php

PHP : Route::get('add-friend', 'yourController@method');

JS : url: /add-friend

Upvotes: 1

umuttaymaz
umuttaymaz

Reputation: 134

If you are doing it with an AJAX call on the views, in my opinion you should do it at web.php. But it should be inside of a route group as named Ajax. You can check out Laravel Documentation for route groups.

Upvotes: 1

Related Questions