mirror of
https://github.com/10h30/Test-Laravel-Eloquent-Basics.git
synced 2026-06-05 15:07:45 +09:00
Test 2 - get data
This commit is contained in:
@@ -31,3 +31,15 @@ In `app/Models/Morningnews.php` file, change it so that the model would work wit
|
||||
|
||||
Test method `test_create_model_incorrect_table()`.
|
||||
|
||||
---
|
||||
|
||||
## Task 2. Eloquent Get Data.
|
||||
|
||||
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:
|
||||
|
||||
```
|
||||
select * from users where email_verified_at is not null order by created_at desc limit 3
|
||||
```
|
||||
|
||||
Test method `test_users_get()`.
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
// TASK: turn this SQL query into Eloquent
|
||||
// select * from users
|
||||
// where email_verified_at is not null
|
||||
// order by created_at desc
|
||||
// limit 3
|
||||
|
||||
$users = User::all(); // replace this with Eloquent statement
|
||||
|
||||
return view('users.index', compact('users'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Email</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach ($users as $user)
|
||||
<tr>
|
||||
<td>{{ $loop->iteration }}. {{ $user->name }}</td>
|
||||
<td>{{ $user->email }}</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -16,3 +16,5 @@ use Illuminate\Support\Facades\Route;
|
||||
Route::get('/', function () {
|
||||
return view('welcome');
|
||||
});
|
||||
|
||||
Route::get('users', [\App\Http\Controllers\UserController::class, 'index']);
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Tests\Feature;
|
||||
|
||||
use App\Models\Morningnews;
|
||||
use App\Models\News;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -20,4 +21,27 @@ class EloquentTest extends TestCase
|
||||
$this->assertDatabaseHas('morning_news', $article);
|
||||
}
|
||||
|
||||
// TASK: Write Eloquent query to return the newest 3 verified users
|
||||
public function test_users_get()
|
||||
{
|
||||
$user1 = User::factory()->create(['created_at' => now()->subMinutes(5)]);
|
||||
$user2 = User::factory()->create(['created_at' => now()->subMinutes(4)]);
|
||||
$user3 = User::factory()->create(['created_at' => now()->subMinutes(3), 'email_verified_at' => NULL]);
|
||||
$user4 = User::factory()->create(['created_at' => now()->subMinutes(2)]);
|
||||
$user5 = User::factory()->create(['created_at' => now()->subMinute()]);
|
||||
|
||||
$response = $this->get('users');
|
||||
|
||||
// This one should be filtered by "email_verified_at is not null"
|
||||
$response->assertDontSee($user3->name);
|
||||
|
||||
// This one should be filtered out by "limit 3"
|
||||
$response->assertDontSee($user1->name);
|
||||
|
||||
// Do we have the correct order?
|
||||
$response->assertSee('1. ' . $user5->name);
|
||||
$response->assertSee('2. ' . $user4->name);
|
||||
$response->assertSee('3. ' . $user2->name); // not $user3
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user