Tiếp nối phần Frontend bằng Angular nay mình học Laravel 8 ( Backend )
19th Aug 2021Hôm nay mình sẽ tiếp phần trước về Frontend Angular
1 – Create project
composer create-project --prefer-dist laravel/laravel backend
2 – Configure Database
.env DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=name_database DB_USERNAME=user_database DB_PASSWORD='password_database'
3 – Create migrate
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'); $table->string('email'); $table->bigInteger('phone'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('person'); } }
Run migrate
php artisan migrate
4 – Create Model
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', 'email', 'phone' ]; }
5 – Create Controller
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; Use Log; class PersonController extends Controller { // https://carbon.now.sh/ public function getAll(){ $data = Person::get(); return response()->json($data, 200); } public function create(Request $request){ $data['name'] = $request['name']; $data['email'] = $request['email']; $data['phone'] = $request['phone']; Person::create($data); return response()->json([ 'message' => "Successfully created", 'success' => true ], 200); } public function delete($id){ $res = Person::find($id)->delete(); return response()->json([ 'message' => "Successfully deleted", 'success' => true ], 200); } public function get($id){ $data = Person::find($id); return response()->json($data, 200); } public function update(Request $request,$id){ $data['name'] = $request['name']; $data['email'] = $request['email']; $data['phone'] = $request['phone']; Person::find($id)->update($data); return response()->json([ 'message' => "Successfully updated", 'success' => true ], 200); } }
6 – Create routes
routes/api.php
use App\Http\Controllers\API\PersonController; Route::prefix('person')->group(function () { Route::get('/',[ PersonController::class, 'getAll']); Route::post('/',[ PersonController::class, 'create']); Route::delete('/{id}',[ PersonController::class, 'delete']); Route::get('/{id}',[ PersonController::class, 'get']); Route::put('/{id}',[ PersonController::class, 'update']); });
Source full cho bài Tuts này, các bạn download ở phần 1
Add new comment