diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 5877c736..e4ed3bd1 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -26,6 +26,10 @@ public function mass_update(Request $request) // where name = $request->old_name // Insert Eloquent statement below + User::where('name', $request->old_name) + ->update([ + 'name' => $request->new_name + ]); return redirect('/')->with('success', 'Projects updated'); } @@ -35,7 +39,9 @@ public function destroy($projectId) Project::destroy($projectId); // TASK: change this Eloquent statement to include the soft-deletes records - $projects = Project::all(); + $projects = Project::withTrashed() + ->where('id', $projectId) + ->restore();; return view('projects.index', compact('projects')); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 9b8c73dc..4b3e86c8 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -15,14 +15,17 @@ public function index() // order by created_at desc // limit 3 - $users = User::all(); // replace this with Eloquent statement + $users = User::whereNot('email_verified_at', NULL) + ->orderBy('created_at', 'desc') + ->limit('3') + ->get(); // replace this with Eloquent statement return view('users.index', compact('users')); } 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')); } @@ -31,8 +34,9 @@ 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 = User::firstOrNew(['name' => $name, 'email' => $email]); + $user->password(Hash::make(Str::rand(8))); + $user->save(); return view('users.show', compact('user')); } @@ -40,7 +44,7 @@ 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 = User::UpdateOrCreate(['name' => 'name'], ['email' => $email, 'password' => Hash::make(Str::rand(8))]); // updated or created user return view('users.show', compact('user')); } @@ -52,6 +56,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/Project.php b/app/Models/Project.php index dc43be0e..e6b2d599 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -9,4 +9,6 @@ class Project extends Model { use HasFactory, SoftDeletes; + + protected $fillable = ['name'] } diff --git a/app/Models/User.php b/app/Models/User.php index e23e0905..2f9c4329 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -41,4 +41,10 @@ class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + + public function scopeActive(Builder $query): void + { + $query->whereNot('email_verified_at', NULL); + } + } diff --git a/app/Observers/ProjectObserver.php b/app/Observers/ProjectObserver.php new file mode 100644 index 00000000..c431ffbf --- /dev/null +++ b/app/Observers/ProjectObserver.php @@ -0,0 +1,52 @@ +projects_count+1; + $stat->save(); + } + + /** + * Handle the User "updated" event. + */ + public function updated(User $user): void + { + // ... + } + + /** + * Handle the User "deleted" event. + */ + public function deleted(User $user): void + { + // ... + } + + /** + * Handle the User "restored" event. + */ + public function restored(User $user): void + { + // ... + } + + /** + * Handle the User "forceDeleted" event. + */ + public function forceDeleted(User $user): void + { + // ... + } +} \ No newline at end of file diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index a9f10a63..a88023c1 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -2,10 +2,12 @@ namespace App\Providers; +use App\Models\Project; use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Support\Facades\Event; +use App\Observers\ProjectObserver; class EventServiceProvider extends ServiceProvider { @@ -28,5 +30,6 @@ class EventServiceProvider extends ServiceProvider public function boot() { // + Project::observe(ProjectObserver::class); } }