mirror of
https://github.com/10h30/Test-Laravel-Eloquent-Basics.git
synced 2026-06-05 15:07:45 +09:00
Task 3 - get a single record
This commit is contained in:
@@ -33,7 +33,7 @@ Test method `test_create_model_incorrect_table()`.
|
||||
|
||||
---
|
||||
|
||||
## Task 2. Eloquent Get Data.
|
||||
## Task 2. Get Data List.
|
||||
|
||||
In `app/Http/Controllers/UserController.php` file method `index()`, write Eloquent query to get 3 newest users with verified emails, ordered from newest to oldest. Transform this SQL query into Eloquent:
|
||||
|
||||
@@ -41,5 +41,13 @@ In `app/Http/Controllers/UserController.php` file method `index()`, write Eloque
|
||||
select * from users where email_verified_at is not null order by created_at desc limit 3
|
||||
```
|
||||
|
||||
Test method `test_users_get()`.
|
||||
Test method `test_get_filtered_list()`.
|
||||
|
||||
---
|
||||
|
||||
## Task 3. Get a Single Record.
|
||||
|
||||
In `app/Http/Controllers/UserController.php` file method `show($userId)`, fill in the `$user` value with finding the user by `users.id = $userId`. If the user is not found, show default Laravel 404 page.
|
||||
|
||||
Test method `test_find_user_or_show_404_page()`.
|
||||
|
||||
|
||||
@@ -18,4 +18,11 @@ class UserController extends Controller
|
||||
|
||||
return view('users.index', compact('users'));
|
||||
}
|
||||
|
||||
public function show($userId)
|
||||
{
|
||||
$user = NULL; // TASK: find user by $userId or show "404 not found" page
|
||||
|
||||
return view('users.show', compact('user'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
<h2>User: {{ $user->name }}</h2>
|
||||
|
||||
Lorem ipsum dummy text.
|
||||
+3
-1
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use App\Http\Controllers\UserController;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
@@ -17,4 +18,5 @@ Route::get('/', function () {
|
||||
return view('welcome');
|
||||
});
|
||||
|
||||
Route::get('users', [\App\Http\Controllers\UserController::class, 'index']);
|
||||
Route::get('users', [UserController::class, 'index']);
|
||||
Route::get('users/{userId}', [UserController::class, 'show']);
|
||||
|
||||
@@ -22,7 +22,7 @@ class EloquentTest extends TestCase
|
||||
}
|
||||
|
||||
// TASK: Write Eloquent query to return the newest 3 verified users
|
||||
public function test_users_get()
|
||||
public function test_get_filtered_list()
|
||||
{
|
||||
$user1 = User::factory()->create(['created_at' => now()->subMinutes(5)]);
|
||||
$user2 = User::factory()->create(['created_at' => now()->subMinutes(4)]);
|
||||
@@ -44,4 +44,15 @@ class EloquentTest extends TestCase
|
||||
$response->assertSee('3. ' . $user2->name); // not $user3
|
||||
}
|
||||
|
||||
public function test_find_user_or_show_404_page()
|
||||
{
|
||||
$response = $this->get('users/1');
|
||||
$response->assertStatus(404);
|
||||
|
||||
$user = User::factory()->create();
|
||||
$response = $this->get('users/1');
|
||||
$response->assertStatus(200);
|
||||
$response->assertViewHas('user', $user);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user