From 4271b97d2803400b1c1db3a509f405bd10c3f712 Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Sat, 26 Oct 2024 01:48:05 +0800 Subject: [PATCH 1/9] 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 6ef9095a2b70f781f914be9eb174f8b6dea2daa8 Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Sat, 26 Oct 2024 01:55:15 +0800 Subject: [PATCH 2/9] Update UserController.php --- app/Http/Controllers/UserController.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 9b8c73dc..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 { @@ -15,14 +16,14 @@ 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')); } 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 5724fcf1e068e4fef0f47e2511f3a138bc93af8b Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Sat, 26 Oct 2024 01:57:24 +0800 Subject: [PATCH 3/9] Update ProjectController.php --- app/Http/Controllers/ProjectController.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 5877c736..25295030 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -11,10 +11,14 @@ class ProjectController extends Controller public function store(Request $request) { // TASK: Currently this statement fails. Fix the underlying issue. - Project::create([ - 'name' => $request->name - ]); + // Project::create([ + // 'name' => $request->name + // ]); + $project=new Project(); + $project->name=$request->name; + $project->save(); + return redirect('/')->with('success', 'Project created'); } @@ -26,6 +30,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 54dc0f3be143be2bfe3410057dc47f3c130e0379 Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Sat, 26 Oct 2024 01:57:51 +0800 Subject: [PATCH 4/9] 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 03c008f54477e20b7e4384c9f1bbc6135c7d619e Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Sat, 26 Oct 2024 01:59:52 +0800 Subject: [PATCH 5/9] 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 6f08dee83cad4923174511b6f52195f0b6719f7f Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Sat, 26 Oct 2024 02:00:25 +0800 Subject: [PATCH 6/9] 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 25295030..efcb4fb5 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -42,7 +42,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 153fcdc3532bc0b0ad06657171f1c27d7e1aa4d1 Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Sat, 26 Oct 2024 02:02:23 +0800 Subject: [PATCH 7/9] 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 6899ff8345a623bc25c9caecd6fdd66240c0c49e Mon Sep 17 00:00:00 2001 From: OhmygoodR <109280933+OhmygoodR@users.noreply.github.com> Date: Sat, 26 Oct 2024 02:05:12 +0800 Subject: [PATCH 8/9] 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..fe49bddb --- /dev/null +++ b/app/Observers/ProjectObserver.php @@ -0,0 +1,20 @@ + Date: Sat, 26 Oct 2024 02:07:17 +0800 Subject: [PATCH 9/9] 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); } }