mirror of
https://github.com/10h30/Test-Laravel-Eloquent-Basics.git
synced 2026-06-05 15:07:45 +09:00
Soft deletes task
This commit is contained in:
@@ -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'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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>
|
||||
@@ -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']);
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user