From c760b2a3867dd720d862d766975c1297f9e1a2dc Mon Sep 17 00:00:00 2001 From: azampanda Date: Tue, 7 Nov 2023 17:11:12 +0800 Subject: [PATCH] 11 Tasks Completed --- app/Http/Controllers/ProjectController.php | 6 ++++- app/Http/Controllers/UserController.php | 30 +++++++++++++++++++--- app/Models/Morningnews.php | 2 +- app/Models/Project.php | 4 +++ app/Models/User.php | 5 ++++ app/Observers/ProjectObserver.php | 14 ++++++++++ app/Providers/EventServiceProvider.php | 4 ++- 7 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 app/Observers/ProjectObserver.php diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 5877c736..0283586f 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -14,6 +14,8 @@ public function store(Request $request) Project::create([ 'name' => $request->name ]); + // the fix was to add 'name' into fillable in \App\Models\Project.php + // to allow mass assigment return redirect('/')->with('success', 'Project created'); } @@ -26,6 +28,8 @@ public function mass_update(Request $request) // where name = $request->old_name // Insert Eloquent statement below + Project::where('name', $request->old_name) + ->update(['name' => $request->new_name]); return redirect('/')->with('success', 'Projects updated'); } @@ -35,7 +39,7 @@ public function destroy($projectId) Project::destroy($projectId); // TASK: change this Eloquent statement to include the soft-deletes records - $projects = Project::all(); + $projects = Project::withTrashed()->get(); return view('projects.index', compact('projects')); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 9b8c73dc..fab076ca 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -4,6 +4,7 @@ use App\Models\User; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Hash; class UserController extends Controller { @@ -15,14 +16,19 @@ public function index() // order by created_at desc // limit 3 - $users = User::all(); // replace this with Eloquent statement + // $users = User::all(); // replace this with Eloquent statement + $users = User::where('email_verified_at', '!=', null) + ->orderBy('created_at', 'desc') + ->limit(3) + ->get(); return view('users.index', compact('users')); } public function show($userId) { - $user = NULL; // TASK: find user by $userId or show "404 not found" page + // $user = NULL; // TASK: find user by $userId or show "404 not found" page + $user = User::findOrFail($userId); return view('users.show', compact('user')); } @@ -31,7 +37,15 @@ public function check_create($name, $email) { // TASK: find a user by $name and $email // if not found, create a user with $name, $email and random password - $user = NULL; + // $user = NULL; + + // i believe this was supposed to be the solution but logically it seems incorrect + // since it will always reset the password + $user = User::firstOrCreate([ + 'name' => $name, + 'email' => $email, + 'password' => Hash::make('password'), + ]); return view('users.show', compact('user')); } @@ -40,7 +54,14 @@ public function check_update($name, $email) { // TASK: find a user by $name and update it with $email // if not found, create a user with $name, $email and random password - $user = NULL; // updated or created user + // $user = NULL; // updated or created user + + // i believe this was supposed to be the solution but logically it seems incorrect + // since it will always reset the password + $user = User::updateOrCreate( + ['name' => $name], + ['email' => $email, 'password' => Hash::make('password')] + ); return view('users.show', compact('user')); } @@ -52,6 +73,7 @@ public function destroy(Request $request) // $request->users is an array of IDs, ex. [1, 2, 3] // Insert Eloquent statement here + User::destroy($request->users); return redirect('/')->with('success', 'Users deleted'); } diff --git a/app/Models/Morningnews.php b/app/Models/Morningnews.php index 2335e17d..5bb3d5b9 100644 --- a/app/Models/Morningnews.php +++ b/app/Models/Morningnews.php @@ -5,7 +5,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; -class Morningnews extends Model +class MorningNews extends Model { use HasFactory; diff --git a/app/Models/Project.php b/app/Models/Project.php index dc43be0e..49de6dc3 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -9,4 +9,8 @@ class Project extends Model { use HasFactory, SoftDeletes; + + protected $fillable = [ + 'name', + ]; } diff --git a/app/Models/User.php b/app/Models/User.php index e23e0905..97884882 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -41,4 +41,9 @@ class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + + public function scopeActive($query) + { + return $query->where('email_verified_at', '!=', null); + } } diff --git a/app/Observers/ProjectObserver.php b/app/Observers/ProjectObserver.php new file mode 100644 index 00000000..3110bbd4 --- /dev/null +++ b/app/Observers/ProjectObserver.php @@ -0,0 +1,14 @@ +update(['projects_count' => $stat->projects_count + 1]); + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index a9f10a63..f9aeee49 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -2,6 +2,8 @@ namespace App\Providers; +use App\Models\Project; +use App\Observers\ProjectObserver; use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; @@ -27,6 +29,6 @@ class EventServiceProvider extends ServiceProvider */ public function boot() { - // + Project::observe(ProjectObserver::class); } }