En este tutorial vamos a aprender a crear una CRUD usando Laravel 10 y la base de datos PostgreSQL. Crearemos una API REST que desarrolle las peticiones CRUD (Create, Read, Update, Delete) usando el Framework Backend de php.
1 – Create project / Crear proyecto
composer create-project laravel/laravel backend-laravel
2 – Configure Database / Configurar Base de datos
.env
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=tutofox
DB_USERNAME=postgres
DB_PASSWORD=12345
3 – Create migrate / Crear migracion
php artisan make:migration create_person_table
database/migrations/2021_05_17_011217_create_person_table.php
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreatePersonTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('person', function (Blueprint $table) { $table->id(); $table->string('name',100); $table->string('address',200); $table->bigInteger('phone'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('person'); } }
Run migrate / ejecutar migrate
php artisan migrate
4 – Create Model / Crear Modelo
php artisan make:model Person
app/Models/Person.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Person extends Model { use HasFactory; protected $table = "person"; protected $fillable = [ 'name', 'address', 'phone' ]; }
5 – Create Controller / Crear Controlador
php artisan make:controller API/PersonController
app/Http/Controllers/API/PersonController.php
<?php namespace App\Http\Controllers\API; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Models\Person; class PersonController extends Controller { public function get(){ try { $data = Person::get(); return response()->json($data, 200); } catch (\Throwable $th) { return response()->json([ 'error' => $th->getMessage()], 500); } } public function create(Request $request){ try { $data['name'] = $request['name']; $data['address'] = $request['address']; $data['phone'] = $request['phone']; $res = Person::create($data); return response()->json( $res, 200); } catch (\Throwable $th) { return response()->json([ 'error' => $th->getMessage()], 500); } } public function getById($id){ try { $data = Person::find($id); return response()->json($data, 200); } catch (\Throwable $th) { return response()->json([ 'error' => $th->getMessage()], 500); } } public function update(Request $request,$id){ try { $data['name'] = $request['name']; $data['address'] = $request['address']; $data['phone'] = $request['phone']; Person::find($id)->update($data); $res = Person::find($id); return response()->json( $res , 200); } catch (\Throwable $th) { return response()->json([ 'error' => $th->getMessage()], 500); } } public function delete($id){ try { $res = Person::find($id)->delete(); return response()->json([ "deleted" => $res ], 200); } catch (\Throwable $th) { return response()->json([ 'error' => $th->getMessage()], 500); } } }
6 – Create routes / Crear rutas
routes/api.php
use App\Http\Controllers\API\PersonController; Route::prefix('v1/persons')->group(function () { Route::get('/',[ PersonController::class, 'get']); Route::post('/',[ PersonController::class, 'create']); Route::delete('/{id}',[ PersonController::class, 'delete']); Route::get('/{id}',[ PersonController::class, 'getById']); Route::put('/{id}',[ PersonController::class, 'update']); });
GitHub
https://github.com/artyom-developer/backend-laravel-crud-api-rest