CREATING A STORE API


SETUP

  1. Install dependencies
composer install
  1. Generate application key
php artisan key:generate
  1. Create a mysql database then add the credentials to .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
  1. Run migrations
php artisan migrate
  1. Run seeders
php artisan db:seed
  1. Create laravel/passport client key
php artisan passport:client --personal

FILES

  1. Models are located at app/Models
  2. Migrations are located at database/migrations
  3. Seeders are located at database/seeders
  4. Controllers are located at app/Http/Controllers
  5. Routes are located at routes
  6. /resources is where our frontend reside

IMPORTANT COMMANDS

  1. Creating a model
php artisan make:model Store
  1. Creating a migration file
php artisan make:migration create_stores_table
  1. Create a model and migration in one command
php artisan make:model ModelName -m
  1. Creating a controller
php artisan make:controller StoreController
  1. To migrate the tables
php artisan migrate
  1. Create a route file example rotues/api.php
php artisan install:api
  1. Get route list
php artisan route:list
  1. Creating a seeder
php artisan make:seeder UserSeeder
  1. Running seeder Running the database seeder (all registered seeder)
php artisan db:seed

Running specific seeder

php artisan db:seed --class=UserSeeder

Reset and Ressed

php artisan migrate:refresh --seed

GETTING DATA FROM REQUEST

Certainly! Here are separate examples for accessing different types of data from the $request object in Laravel without including the whole controller.

Example Controller Method

use Illuminate\Http\Request;

class ExampleController extends Controller
{
    public function handleRequest(Request $request, $id)
    {
        // 1. **Route Parameters**: Accessing route parameters
        // Assuming the route is defined as: Route::get('/example/{id}', [ExampleController::class, 'handleRequest']);
        $routeParam = $id; // Accessing the route parameter directly

        // 2. **Query Parameters**: Accessing query parameters from the URL
        // Example: /example?id=123&filter=active
        $queryParamId = $request->query('id'); // Get 'id' from query string
        $filter = $request->query('filter'); // Get 'filter' from query string

        // 3. **Request Body (Form Data)**: Accessing form data sent in the request body
        // Example: Form submission with fields 'name' and 'email'
        $name = $request->input('name'); // Get 'name' from form data
        $email = $request->input('email'); // Get 'email' from form data

        // 4. **Retrieving All Input Data**: Getting all input data
        $allData = $request->all(); // Get all data from both form and query

        // 5. **Retrieving JSON Data**: Accessing JSON data if the request is JSON
        // Assuming a JSON payload like: { "key": "value" }
        $jsonValue = $request->json('key'); // Get a specific key from JSON
        $allJsonData = $request->json()->all(); // Get all JSON data

        // Example response
        return response()->json([
            'route_param' => $routeParam,
            'query_param_id' => $queryParamId,
            'filter' => $filter,
            'name' => $name,
            'email' => $email,
            'all_data' => $allData,
            'json_value' => $jsonValue,
            'all_json_data' => $allJsonData,
        ]);
    }
}

1. Accessing Route Parameters

// Assuming the route is defined as: Route::get('/example/{id}', [ExampleController::class, 'handleRequest']);
$routeParam = $id; // Accessing the route parameter directly

2. Accessing Query Parameters

// Example: /example?id=123&filter=active
$queryParamId = $request->query('id'); // Get 'id' from query string
$filter = $request->query('filter'); // Get 'filter' from query string

3. Accessing Request Body (Form Data)

// Example: Form submission with fields 'name' and 'email'
$name = $request->input('name'); // Get 'name' from form data
$email = $request->input('email'); // Get 'email' from form data

4. Retrieving All Input Data

$allData = $request->all(); // Get all data from both form and query

5. Accessing JSON Data

// Assuming a JSON payload like: { "key": "value" }
$jsonValue = $request->json('key'); // Get a specific key from JSON
$allJsonData = $request->json()->all(); // Get all JSON data