How do I add a following api to my laravel app?
To add a following API to your Laravel app, you'll need to create routes, controllers, models, and possibly middleware for handling following and unfollowing actions. Here's a step-by-step guide to help you get started:
- Define Routes:
First, you need to define routes for following and unfollowing actions. Open the
routes/web.php
file and add the following routes:
Route::middleware(['auth'])->group(function () {
Route::post('/follow/{user}', 'FollowingController@follow')->name('follow');
Route::post('/unfollow/{user}', 'FollowingController@unfollow')->name('unfollow');
});
Replace {user}
with the actual user model name if it's different.
- Create Controllers: Next, create a new controller for handling following and unfollowing actions. Run the following command to create a new controller:
php artisan make:controller FollowingController
Now, open the FollowingController.php
file and add the following methods:
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class FollowingController extends Controller
{
public function follow(Request $request, User $user)
{
auth()->user()->follow($user);
return back();
}
public function unfollow(Request $request, User $user)
{
auth()->user()->unfollow($user);
return back();
}
}
Replace User
with the actual user model name if it's different.
- Implement Following and Unfollowing Methods: Now, you need to implement the following and unfollowing methods in the user model. Run the following command to open the user model file:
php artisan make:model User --model=App\Models\User --force
Then, open the User.php
file and add the following methods:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class User extends Model
{
// ...
public function followers()
{
return $this->belongsToMany(User::class, 'followers', 'user_id', 'follower_id');
}
public function following()
{
return $this->hasMany(User::class, 'follower_id', 'id');
}
public function follow($user)
{
return $this->followers()->attach($user);
}
public function unfollow($user)
{
return $this->followers()->detach($user);
}
}
Replace User
with the actual user model name if it's different.
- Create Middleware:
You may want to add middleware to protect following and unfollowing actions from unauthorized users. Open the
app/Http/Kernel.php
file and add the following middleware to the$routeMiddleware
array:
'following' => \App\Http\Middleware\FollowingMiddleware::class,
Now, create a new middleware file by running the following command:
php artisan make:middleware FollowingMiddleware
Open the FollowingMiddleware.php
file and add the following code:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class FollowingMiddleware
{
public function handle(Request $request, Closure $next, $guard = null)
{
if (auth()->check() && $request->is('follow/*')) {
$user = User::findOrFail(explode('/', $request->path())[2]);
if (auth()->user()->following($user)->count() > 0) {
return redirect()->route('home')->with('error', 'You are already following this user.');
}
}
return $next($request);
}
}
Replace User
with the actual user model name if it's different.
- Update Routes: Finally, update the following routes to use the new middleware:
Route::middleware(['auth', 'following'])->group(function () {
Route::post('/follow/{user}', 'FollowingController@follow')->name('follow');
Route::post('/unfollow/{user}', 'FollowingController@unfollow')->name('unfollow');
});
Now, you should have a working following API in your Laravel app. Users can follow and unfollow other users by making POST requests to the /follow/{user}
and /unfollow/{user}
routes.