diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 5877c736..2441ddbc 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -26,7 +26,7 @@ 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,8 +35,8 @@ 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..3e3c8b47 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -15,23 +15,25 @@ public function index() // order by created_at desc // limit 3 - $users = User::all(); // replace this with Eloquent statement - + $users = User::whereNotNull('email_verified_at')->orderBy('created_at', 'desc')->take(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 - + // TASK: find user by $userId or show "404 not found" page +$user = User::findOrFail($userId); return view('users.show', compact('user')); } public function check_create($name, $email) { - // TASK: find a user by $name and $email + // TASK4: find a user by $name and $email // if not found, create a user with $name, $email and random password - $user = NULL; + $user = User::firstOrNew(['email' => $email], ['name' => $name]); + $user->password = rand(8,12); + $user->save(); return view('users.show', compact('user')); } @@ -40,7 +42,8 @@ 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 + // updated or created user + $user = User::UpdateOrCreate(['name' => $name], ['email' => $email]); // updated or created user return view('users.show', compact('user')); } @@ -53,6 +56,8 @@ public function destroy(Request $request) // 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..827dea26 100644 --- a/app/Models/Morningnews.php +++ b/app/Models/Morningnews.php @@ -8,6 +8,6 @@ class Morningnews extends Model { use HasFactory; - + protected $table = 'morning_news'; protected $fillable = ['title', 'news_text']; } diff --git a/app/Models/Project.php b/app/Models/Project.php index dc43be0e..490ede6b 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -9,4 +9,7 @@ class Project extends Model { use HasFactory, SoftDeletes; + protected $fillable = [ + 'name' + ]; } diff --git a/app/Models/User.php b/app/Models/User.php index e23e0905..d561c6f3 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -41,4 +41,8 @@ class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + public function scopeActive($query) + { + return $query->whereNotNull('email_verified_at'); + } } diff --git a/app/Observers/ProjectObserver.php b/app/Observers/ProjectObserver.php new file mode 100644 index 00000000..d891e4f3 --- /dev/null +++ b/app/Observers/ProjectObserver.php @@ -0,0 +1,49 @@ +string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); - $table->string('password'); + $table->string('password')->default(rand(0,3)); $table->rememberToken(); $table->timestamps(); });