Laravel Blog


Create Laravel Project via terminal
composer create-project --prefer-dist laravel/laravel myblog

Open project folder on visual studio code

First, test the project running well on localhost.
Open terminal on vs code and run :
php artisan serve
open in browser http://127.0.0.1:8000/
Ctrl + C to stop serve

Set Up dependencies
  1. JWT
    • composer require tymon/jwt-auth
    • php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
    • php artisan jwt:secret
  2. Laravel Eloquent Spatial
    • composer require matanyadaev/laravel-eloquent-spatial
Set up access public storage for upload and load image file
php artisan storage:link

Set Up .env file :

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_db_name
DB_USERNAME=root
DB_PASSWORD=

Delete all file inside database/migrations we will create new later

Create User Model inside app/Models
Create migration file for table users
php artisan make:migration create_users_table
Modify migration file for user
Create AuthController class
php artisan make:controller AuthController
Modify AuthController class

Create BlogPost Model app/Models
Create migration file for table blog post
php artisan make:migration create_blog_posts_table
Modify migration file for blog post
Create BlogPostController
php artisan make:controller BlogPostController
Modify BlogPostController

Define routes / end points in routes/api.php
Check route list
php artisan route:list
Migrate database (create db and table)
php artisan migrate

In app/Http/Kernel.php, ensure the JWT middleware is registered:
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth:api' => \Tymon\JWTAuth\Http\Middleware\Authenticate::class,
]; 


Config Auth guards
'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
],

Error when auth guards not set :
InvalidArgumentException: Auth guard [api] is not defined. in file /home/advo/laravel-project/laravel-blog/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 86

Postman Collection
https://github.com/firdausmaulan/laravel-blog-api/blob/main/laravel-blog.postman_collection.json

Sample Image Url
http://127.0.0.1:8000/storage/images/csLsd3ZZueTWFywOYuK9D0BLVqzgHqcuNkThfHtk.png
https://firdaus91.web.id/blog/storage/app/public/images/8vQA7utpr4km9s043B4MRZQt8Wk3i4KBb0QaMVoG.png
if image not loaded try to run
php artisan storage:link

PUT in laravel using POST with suffix ?_method=PUT

Config JWT expiration time
Open config/jwt.php
set ttl to 10080// 7 days in minutes

Error expired JWT :
Symfony\Component\Routing\Exception\RouteNotFoundException: Route [login] not defined. in file /home/advo/laravel-project/laravel-blog-api/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php on line 477

improve return error when JWT expired

UnexpectedValueException: There is no existing directory at /storage/logs
Delete bootstrap/cache/config.php
Delete all log files in storage/logs.

Komentar