From 6de53e285b173e964a07b301db5d8213b8755ced Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Sun, 9 Jun 2024 23:53:47 +0800 Subject: [PATCH 01/10] Update Morningnews.php --- app/Models/Morningnews.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; From c5f9b601834ead6d79b5f4b4f466dba2ef1170f4 Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Sun, 9 Jun 2024 23:55:07 +0800 Subject: [PATCH 02/10] Update UserController.php --- app/Http/Controllers/UserController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 9b8c73dc..e9585635 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -15,7 +15,7 @@ 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')->limit(3)->get(); // replace this with Eloquent statement return view('users.index', compact('users')); } From 5734efc7506c7ca1d7cd68af22fb0ff867ab95ef Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Sun, 9 Jun 2024 23:58:06 +0800 Subject: [PATCH 03/10] Update UserController.php --- app/Http/Controllers/UserController.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index e9585635..4975a6d1 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 { @@ -22,7 +23,7 @@ public function index() 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,7 +32,12 @@ 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::firstOrCreate([ + 'name'=>$name, + 'email'=>$email + ],[ + 'password'=>Hash::make('random_password') + ]); return view('users.show', compact('user')); } From 73b0842e2de8c05324b24198188d379980048864 Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Mon, 10 Jun 2024 00:00:00 +0800 Subject: [PATCH 04/10] Update ProjectController.php --- app/Http/Controllers/ProjectController.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 5877c736..17dfe0a4 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -11,9 +11,12 @@ class ProjectController extends Controller public function store(Request $request) { // TASK: Currently this statement fails. Fix the underlying issue. - Project::create([ - 'name' => $request->name - ]); + + $project=new Project(); + $project->name=$request->name; + $project->save(); + + return redirect('/')->with('success', 'Project created'); } @@ -26,6 +29,9 @@ 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'); } From 8a956c950bc347a39b53a9898dfa02b1090accca Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Mon, 10 Jun 2024 00:02:12 +0800 Subject: [PATCH 05/10] Update UserController.php --- app/Http/Controllers/UserController.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 4975a6d1..8dc56304 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -46,7 +46,12 @@ 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('random_password') + ]); // updated or created user return view('users.show', compact('user')); } @@ -58,6 +63,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'); } From fbe5d0c326ea313d1e38ea44eeb000f50b96a915 Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Mon, 10 Jun 2024 00:02:44 +0800 Subject: [PATCH 06/10] Update Project.php --- app/Models/Project.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Models/Project.php b/app/Models/Project.php index dc43be0e..600c81b2 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' + ]; } From 798dc6557d4dc01099391a8152e153955d0cfe49 Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Mon, 10 Jun 2024 00:03:33 +0800 Subject: [PATCH 07/10] Update ProjectController.php --- app/Http/Controllers/ProjectController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 17dfe0a4..488cc8c6 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -41,7 +41,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')); } From 2b50f45e8a5f04dca96511a14b37147f1f219295 Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Mon, 10 Jun 2024 00:05:52 +0800 Subject: [PATCH 08/10] Update User.php --- app/Models/User.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/Models/User.php b/app/Models/User.php index e23e0905..c5205e68 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -7,6 +7,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; +use Illuminate\Database\Eloquent\Builder; class User extends Authenticatable { @@ -41,4 +42,8 @@ class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + + public function scopeActive(Builder $query){ + $query->whereNotNull('email_verified_at'); + } } From 00bdc1c8875a7bbf7c144d06adadfe34312a5f02 Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Mon, 10 Jun 2024 00:08:57 +0800 Subject: [PATCH 09/10] Create ProjectObserver.php --- app/Observers/ProjectObserver.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 app/Observers/ProjectObserver.php diff --git a/app/Observers/ProjectObserver.php b/app/Observers/ProjectObserver.php new file mode 100644 index 00000000..56f277a4 --- /dev/null +++ b/app/Observers/ProjectObserver.php @@ -0,0 +1,20 @@ + Date: Mon, 10 Jun 2024 00:09:55 +0800 Subject: [PATCH 10/10] Update AppServiceProvider.php --- app/Providers/AppServiceProvider.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ee8ca5bc..048f7fd5 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -3,6 +3,8 @@ namespace App\Providers; use Illuminate\Support\ServiceProvider; +use App\Models\Project; +use App\Observers\ProjectObserver; class AppServiceProvider extends ServiceProvider { @@ -24,5 +26,6 @@ public function register() public function boot() { // + Project::observe(ProjectObserver::class); } }