Complete Task 4

This commit is contained in:
Thuan Bui
2025-04-04 08:49:35 +09:00
parent bd0112fc11
commit fcea780b69
+7 -3
View File
@@ -4,6 +4,8 @@ namespace App\Http\Controllers;
use App\Models\User; use App\Models\User;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class UserController extends Controller class UserController extends Controller
{ {
@@ -22,7 +24,7 @@ class UserController extends Controller
public function show($userId) public function show($userId)
{ {
$user = NULL; // TASK: find user by $userId or show "404 not found" page $user = User::findOrFail($userId); // TASK: find user by $userId or show "404 not found" page
return view('users.show', compact('user')); return view('users.show', compact('user'));
} }
@@ -31,7 +33,10 @@ class UserController extends Controller
{ {
// TASK: find a user by $name and $email // TASK: find a user by $name and $email
// if not found, create a user with $name, $email and random password // if not found, create a user with $name, $email and random password
$user = NULL; $user = User::firstOrCreate(
['name' => $name, 'email' => $email],
['password' => Hash::make(Str::random(12))]
);
return view('users.show', compact('user')); return view('users.show', compact('user'));
} }
@@ -64,5 +69,4 @@ class UserController extends Controller
return view('users.index', compact('users')); return view('users.index', compact('users'));
} }
} }