mirror of
https://github.com/10h30/Test-Laravel-Eloquent-Basics.git
synced 2026-06-05 15:07:45 +09:00
Task 6 - mass update
This commit is contained in:
@@ -16,4 +16,16 @@ class ProjectController extends Controller
|
||||
|
||||
return redirect('/')->with('success', 'Project created');
|
||||
}
|
||||
|
||||
public function mass_update(Request $request)
|
||||
{
|
||||
// TASK: Transform this SQL query into Eloquent
|
||||
// update projects
|
||||
// set name = $request->new_name
|
||||
// where name = $request->old_name
|
||||
|
||||
// Insert Eloquent statement below
|
||||
|
||||
return redirect('/')->with('success', 'Projects updated');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +19,12 @@ Route::get('/', function () {
|
||||
return view('welcome');
|
||||
});
|
||||
|
||||
// NOTICE: Not all the routes are logical and would exist in a real Laravel project
|
||||
// Some routes are just for the purpose of replicating some testing scenario
|
||||
|
||||
Route::get('users', [UserController::class, 'index']);
|
||||
Route::get('users/{userId}', [UserController::class, 'show']);
|
||||
Route::get('users/check/{name}/{email}', [UserController::class, 'check_create']);
|
||||
|
||||
Route::post('projects', [ProjectController::class, 'store']);
|
||||
Route::post('projects/mass_update', [ProjectController::class, 'mass_update']);
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Tests\Feature;
|
||||
|
||||
use App\Models\Morningnews;
|
||||
use App\Models\News;
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
@@ -74,4 +75,19 @@ class EloquentTest extends TestCase
|
||||
$response->assertRedirect();
|
||||
}
|
||||
|
||||
public function test_mass_update_projects() {
|
||||
$project = new Project();
|
||||
$project->name = 'Old name';
|
||||
$project->save();
|
||||
|
||||
$this->assertDatabaseHas('projects', ['name' => 'Old name']);
|
||||
|
||||
$response = $this->post('projects/mass_update', [
|
||||
'old_name' => 'Old name',
|
||||
'new_name' => 'New name'
|
||||
]);
|
||||
$response->assertRedirect();
|
||||
$this->assertDatabaseMissing('projects', ['name' => 'Old name']);
|
||||
$this->assertDatabaseHas('projects', ['name' => 'New name']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user