https://github.com/basemax/firstlaravelapi
A simple example of how to create a RESTful API in Laravel Framework 8.36.1.
https://github.com/basemax/firstlaravelapi
laravel laravel-example laravel-json-api laravel-rest laravel-rest-api laravel-restful-api laravel-sample php php8
Last synced: about 2 months ago
JSON representation
A simple example of how to create a RESTful API in Laravel Framework 8.36.1.
- Host: GitHub
- URL: https://github.com/basemax/firstlaravelapi
- Owner: BaseMax
- Created: 2021-04-06T21:32:41.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-11-13T06:38:28.000Z (8 months ago)
- Last Synced: 2025-05-04T21:45:38.425Z (about 2 months ago)
- Topics: laravel, laravel-example, laravel-json-api, laravel-rest, laravel-rest-api, laravel-restful-api, laravel-sample, php, php8
- Language: PHP
- Homepage:
- Size: 9.3 MB
- Stars: 12
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# First Laravel PHP
A simple example of how to create a RESTful API in Laravel Framework 8.36.1.
I used Database **sqlite** because I wanted to deploy this project very quickly and easy.
You can easily change the type of database and its connection in the **[config file](.env)**.## Endpoints
**Get all posts:** `GET /api/posts`
**Get a single post:** `GET /api/posts/{id}`
**Create a new post:** `POST /api/posts`
**Update a post:** `PUT /api/posts/{id}`
**Delete a post:** `DELETE /api/posts/{id}`
## Routes
```
Route::get('/posts', [PostsApiController::class, 'index']);
Route::get('/post/{post}', [PostsApiController::class, 'get']);
Route::post('/posts', [PostsApiController::class, 'store']);
Route::put('/posts/{post}', [PostsApiController::class, 'update']);
Route::delete('/posts/{post}', [PostsApiController::class, 'destroy']);
```### Laravel artisan commands
```
php artisan --version
rm database/database.sqlite
sqlite3 database/database.sqlite "create table aTable(field1 int); drop table aTable;"
php artisan make:model Post -m
php artisan migrate
php artisan migrate:fresh
php artisan migrate:status
php artisan migrate:reset
php artisan migrate:refresh
php artisan migrate
php artisan make:controller PostsApiController
```### Insert fake/sample data to database
**$ php artisan tinker**
```
Psy Shell v0.10.7 (PHP 8.0.3 — cli) by Justin Hileman
>>> $post = new Post;
[!] Aliasing 'Post' to 'App\Models\Post' for this Tinker session.
=> App\Models\Post {#3329}
>>> $post->title = 'My first post'
=> "My first post"
>>> $post->content = 'My first blog post text...'
=> "My first blog post text..."
>>> $post->save()
=> true>>> $post = new Post
=> App\Models\Post {#3322}
>>> $post->title = 'My second post'
=> "My second post"
>>> $post->content = 'My second blog post text'
=> "My second blog post text"
>>> $post->save()
=> true
```### Database Preview


## Acknowledgment
I saw an Youtube video and It's encouraged me to write a similar project myself.
© Copyright Max Base 2021