Skip to content

arslanramay/laravel-trips-api

Repository files navigation

Laravel Trip Planning API

Overview

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.

Prerequisites

  • PHP 8.3+
  • Composer
  • MySQL 8
  • Laravel 11
  • Node.js & npm (for frontend if needed)

Installation Steps

  1. Clone the Repository

    git clone https://github.com/arslanramay/laravel-trips-api.git
    cd laravel-trips-api
  2. Install Dependencies

    composer install
  3. Create .env File

    cp .env.example .env

    Update .env with your database credentials.

  4. Generate Application Key

    php artisan key:generate
  5. Run Migrations

    php artisan migrate

Setting Up Laravel Sanctum

Laravel Sanctum is used for API authentication.

  1. Install Sanctum (if not installed)

    composer require laravel/sanctum
  2. Publish Sanctum Configuration

    php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
  3. Run Database Migrations

    php artisan migrate
  4. Add Sanctum Middleware in app/Http/Kernel.php

    protected $middlewareGroups = [
        'api' => [
            \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];
  5. Add Sanctum Trait in User Model

    use Laravel\Sanctum\HasApiTokens;
    
    class User extends Authenticatable {
        use HasApiTokens, HasFactory, Notifiable;
    }
  6. Authenticate Users via API Token

    • Login API generates a Bearer token to be used in further requests.
    • Include Authorization: Bearer {token} in API calls.

Running Database Seeders

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

API Endpoints

Authentication

  • POST /api/login - Logs in a user and returns an API token.
  • POST /api/register - Registers a new user.

Cities Search

  • GET /api/cities?temp_min=15&temp_max=30&month=June - Fetches cities based on weather conditions.

Trips Management

  • POST /api/trips - Stores trips for the authenticated user.
  • GET /api/trips - Retrieves user’s planned trips.

Running the Application

  1. Start Laravel Server

    php artisan serve
  2. Test API with Postman

    • Set Authorization: Bearer {token} in the headers.
    • Use the provided API endpoints to interact with the system.

Deployment

  1. Optimize for Production
    php artisan config:cache
    php artisan route:cache
    php artisan view:cache

About

Trip Planning Application API based on PHP Laravel

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages