Soft deletes task

This commit is contained in:
PovilasKorop
2021-11-16 10:45:45 +02:00
parent a5acfdd85f
commit 328e51c4ee
7 changed files with 45 additions and 1 deletions
+8
View File
@@ -96,3 +96,11 @@ In `app/Http/Controllers/UserController.php` file method `destroy()`, delete all
Test method `test_mass_delete_users()`.
---
## Task 9. Soft Deletes.
In `app/Http/Controllers/ProjectController.php` file method `destroy()`, change Eloquent statement to still return the soft-deleted records in the list of `$projects`
Test method `test_soft_delete_projects()`.
---
@@ -28,4 +28,14 @@ class ProjectController extends Controller
return redirect('/')->with('success', 'Projects updated');
}
public function destroy($projectId)
{
Project::destroy($projectId);
// TASK: change this Eloquent statement to include the soft-deletes records
$projects = Project::all();
return view('projects.index', compact('projects'));
}
}
+2 -1
View File
@@ -4,8 +4,9 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Project extends Model
{
use HasFactory;
use HasFactory, SoftDeletes;
}
@@ -17,6 +17,7 @@ class CreateProjectsTable extends Migration
$table->id();
$table->string('name');
$table->timestamps();
$table->softDeletes();
});
}
+14
View File
@@ -0,0 +1,14 @@
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
@foreach ($projects as $project)
<tr>
<td>{{ $loop->iteration }}. {{ $project->name }}</td>
</tr>
@endforeach
</tbody>
</table>
+1
View File
@@ -30,3 +30,4 @@ Route::delete('users', [UserController::class, 'destroy']);
Route::post('projects', [ProjectController::class, 'store']);
Route::post('projects/mass_update', [ProjectController::class, 'mass_update']);
Route::delete('projects/{projectId}', [ProjectController::class, 'destroy']);
+9
View File
@@ -119,4 +119,13 @@ class EloquentTest extends TestCase
$this->assertDatabaseCount('users', 1);
}
public function test_soft_delete_projects()
{
$project = new Project();
$project->name = 'Some name';
$project->save();
$response = $this->delete('projects/' . $project->id);
$response->assertSee('Some name');
}
}