mirror of
https://github.com/10h30/Test-Laravel-Eloquent-Basics.git
synced 2026-06-05 15:07:45 +09:00
Task 11 - observers
This commit is contained in:
@@ -112,3 +112,11 @@ In `app/Http/Controllers/UserController.php` file method `only_active()`, make t
|
||||
Test method `test_active_users()`.
|
||||
|
||||
---
|
||||
|
||||
## Task 11. Observers with New Record.
|
||||
|
||||
In `app/Http/Controllers/ProjectController.php` file method `store_with_stats()`, create a separate Observer file with an event to perform a +1 in the stats table.
|
||||
|
||||
Test method `test_insert_observer()`.
|
||||
|
||||
---
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\Stat;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ProjectController extends Controller
|
||||
@@ -38,4 +39,16 @@ class ProjectController extends Controller
|
||||
|
||||
return view('projects.index', compact('projects'));
|
||||
}
|
||||
|
||||
public function store_with_stats(Request $request)
|
||||
{
|
||||
// TASK: on creating a new project, create an Observer event to run SQL
|
||||
// update stats set projects_count = projects_count + 1
|
||||
$project = new Project();
|
||||
$project->name = $request->name;
|
||||
$project->save();
|
||||
|
||||
return redirect('/')->with('success', 'Project created');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Stat extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['users_count', 'projects_count'];
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateStatsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('stats', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->integer('users_count')->default(0);
|
||||
$table->integer('projects_count')->default(0);
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
\App\Models\Stat::create(['users_count' => 0, 'projects_count' => 0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('stats');
|
||||
}
|
||||
}
|
||||
@@ -30,5 +30,6 @@ Route::get('users/check_update/{name}/{email}', [UserController::class, 'check_u
|
||||
Route::delete('users', [UserController::class, 'destroy']);
|
||||
|
||||
Route::post('projects', [ProjectController::class, 'store']);
|
||||
Route::post('projects/stats', [ProjectController::class, 'store_with_stats']);
|
||||
Route::post('projects/mass_update', [ProjectController::class, 'mass_update']);
|
||||
Route::delete('projects/{projectId}', [ProjectController::class, 'destroy']);
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Tests\Feature;
|
||||
use App\Models\Morningnews;
|
||||
use App\Models\News;
|
||||
use App\Models\Project;
|
||||
use App\Models\Stat;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
@@ -136,4 +137,12 @@ class EloquentTest extends TestCase
|
||||
$response = $this->get('users/active');
|
||||
$response->assertDontSee($user->name);
|
||||
}
|
||||
|
||||
public function test_insert_observer()
|
||||
{
|
||||
$this->post('projects/stats', ['name' => 'Some name']);
|
||||
|
||||
$statsRow = Stat::first();
|
||||
$this->assertEquals(1, $statsRow->projects_count);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user