This is a Laravel-based API application that allows users to search for cities based on weather conditions, store planned trips, and manage user authentication via Laravel Sanctum.
- PHP 8.3+
- Composer
- MySQL 8
- Laravel 11
- Node.js & npm (for frontend if needed)
-
Clone the Repository
git clone https://github.com/arslanramay/laravel-trips-api.git cd laravel-trips-api
-
Install Dependencies
composer install
-
Create
.env
Filecp .env.example .env
Update
.env
with your database credentials. -
Generate Application Key
php artisan key:generate
-
Run Migrations
php artisan migrate
Laravel Sanctum is used for API authentication.
-
Install Sanctum (if not installed)
composer require laravel/sanctum
-
Publish Sanctum Configuration
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
-
Run Database Migrations
php artisan migrate
-
Add Sanctum Middleware in
app/Http/Kernel.php
protected $middlewareGroups = [ 'api' => [ \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class, 'throttle:api', \Illuminate\Routing\Middleware\SubstituteBindings::class, ], ];
-
Add Sanctum Trait in User Model
use Laravel\Sanctum\HasApiTokens; class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable; }
-
Authenticate Users via API Token
- Login API generates a Bearer token to be used in further requests.
- Include
Authorization: Bearer {token}
in API calls.
To populate the database with test data, run:
php artisan db:seed
To seed a specific table, use:
php artisan db:seed --class=UserSeeder
php artisan db:seed --class=CitySeeder
php artisan db:seed --class=WeatherPreferenceSeeder
php artisan db:seed --class=WeatherConditionSeeder
php artisan db:seed --class=TripSeeder
POST /api/login
- Logs in a user and returns an API token.POST /api/register
- Registers a new user.
GET /api/cities?temp_min=15&temp_max=30&month=June
- Fetches cities based on weather conditions.
POST /api/trips
- Stores trips for the authenticated user.GET /api/trips
- Retrieves user’s planned trips.
-
Start Laravel Server
php artisan serve
-
Test API with Postman
- Set
Authorization: Bearer {token}
in the headers. - Use the provided API endpoints to interact with the system.
- Set
- Optimize for Production
php artisan config:cache php artisan route:cache php artisan view:cache