Laravel 8 REST API

Create application with laravel 8. Set up the mysql connection in .env conf file.

we have test a table:

php artisan make:migration create_blogs_table

migration def:

    public function up() {
        Schema::create('blogs', function (Blueprint $table) {
            $table->id();
            $table->string('title', 255)->nullable();
            $table->text('body')->nullable();
            $table->timestamps();
        });
    }
php artisan migrate

Add Resource Route in routes/api.php

use App\Http\Controllers\BlogController;
Route::resource('blogs', BlogController::class);

we generate the corresponding model and controller with:

php artisan make:controller BlogController --resourcemodel=Blog

controller will look like this:

namespace App\Http\Controllers;
use App\Models\Blog;
use Illuminate\Http\Request;
class BlogController extends Controller {
    /**
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
     */
    public function index() {
        $blogs = Blog::latest()->paginate(10);
        return [
            "status" => 1,
            "data" => $blogs
        ];
    }
    /**
     * Show the form for creating a new resource.
     * @return \Illuminate\Http\Response
     */
    public function create() {
    }
    /**
     * Store a newly created resource in storage.
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
        $request->validate([
            'title' => 'required',
            'body' => 'required',
        ]);
        $blog = Blog::create($request->all());
        return [
            "status" => 1,
            "data" => $blog
        ];
    }
    /**
     * Display the specified resource.
     * @param  \App\Models\Blog  $blog
     * @return \Illuminate\Http\Response
     */
    public function show(Blog $blog) {
        return [
            "status" => 1,
            "data" =>$blog
        ];
    }
    /**
     * Show the form for editing the specified resource.
     * @param  \App\Models\Blog  $blog
     * @return \Illuminate\Http\Response
     */
    public function edit(Blog $blog) {
    }
    /**
     * Update the specified resource in storage.
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Blog  $blog
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Blog $blog) {
        $request->validate([
            'title' => 'required',
            'body' => 'required',
        ]);
        $blog->update($request->all());
        return [
            "status" => 1,
            "data" => $blog,
            "msg" => "Blog updated successfully"
        ];
    }
    /**
     * Remove the specified resource from storage.
     * @param  \App\Models\Blog  $blog
     * @return \Illuminate\Http\Response
     */
    public function destroy(Blog $blog) {
        $blog->delete();
        return [
            "status" => 1,
            "data" => $blog,
            "msg" => "Blog deleted successfully"
        ];
    }
}

after

php artisan serve

the api controller responds at /blogs URL

# create
curl -d "title=title1&body=value2" -X POST http://localhost:8080/blogs
 
# update
http://127.0.0.1:8000/blogs/{id}
method: PUT/PATCH
curl -d "title=titleUP&body=valueUP" -X PUT http://localhost:8080/blogs/{ID}
 
# get list of all
# method: GET
curl http://127.0.0.1:8000/blogs/
 
 
# get a single entity
# method: GET
curl http://127.0.0.1:8000/blogs/{id}
 
# delete an entity
curl -X DELETE http://127.0.0.1:8000/blogs/{id}

Tags:
PHP Laravel

Blog Disclaimer:

Le opinioni espresse nel mio blog sono solo questo: mie opinioni.

In nessun modo rappresento le opinioni dei miei clienti in questa sede.


Notice: Undefined variable: browserName in /var/www/taziomirandola.it/lib/Visitors.php on line 86

Notice: Undefined variable: browserName in /var/www/taziomirandola.it/lib/Visitors.php on line 96

Deprecated: strripos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior in /var/www/taziomirandola.it/lib/Visitors.php on line 96

Notice: Undefined index: HTTP_ACCEPT_LANGUAGE in /var/www/taziomirandola.it/lib/Visitors.php on line 39

Fatal error: Uncaught TypeError: Argument 1 passed to safe_text() must be of the type string, null given, called in /var/www/taziomirandola.it/lib/Visitors.php on line 39 and defined in /var/www/taziomirandola.it/lib/Visitors.php:162 Stack trace: #0 /var/www/taziomirandola.it/lib/Visitors.php(39): safe_text() #1 /var/www/taziomirandola.it/lib/Visitors.php(124): Visitors::getData() #2 [internal function]: Visitors::log() #3 {main} thrown in /var/www/taziomirandola.it/lib/Visitors.php on line 162