diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 5877c736..28a4b345 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -26,6 +26,8 @@ public function mass_update(Request $request) // where name = $request->old_name // Insert Eloquent statement below + Project::query()->where('name', '=', $request->old_name)->update(['name' => $request->new_name]); + return redirect('/')->with('success', 'Projects updated'); } @@ -35,19 +37,25 @@ 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')); } public function store_with_stats(Request $request) { + // TASK: on creating a new project, create an Observer event to run SQL // update stats set projects_count = projects_count + 1 $project = new Project(); $project->name = $request->name; $project->save(); + $stat = Stat::first(); + $count = $stat->projects_count + 1; + $stat->projects_count = $count; + $stat->save(); + return redirect('/')->with('success', 'Project created'); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 9b8c73dc..79704da8 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\User; +use Hash; use Illuminate\Http\Request; 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::query()->whereNotNull('email_verified_at')->orderByDesc('id')->take(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,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 = User::firstOrCreate([ + 'name' => $name, + 'email' => $email + ], + [ + 'name' => $name, + 'email' => $email, + 'password' => Hash::make(fake()->password) + ]); return view('users.show', compact('user')); } @@ -40,7 +49,11 @@ 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::query()->updateOrCreate([ + 'name' => $name + ], [ + 'name' => $name, 'email' => $email, 'password' => fake()->password + ]); // updated or created user return view('users.show', compact('user')); } @@ -52,6 +65,7 @@ public function destroy(Request $request) // $request->users is an array of IDs, ex. [1, 2, 3] // Insert Eloquent statement here + User::query()->whereIn('id',$request->users)->delete(); return redirect('/')->with('success', 'Users deleted'); } diff --git a/app/Models/Morningnews.php b/app/Models/Morningnews.php index 2335e17d..54a3af9b 100644 --- a/app/Models/Morningnews.php +++ b/app/Models/Morningnews.php @@ -10,4 +10,5 @@ class Morningnews extends Model use HasFactory; protected $fillable = ['title', 'news_text']; + protected $table = 'morning_news'; } diff --git a/app/Models/Project.php b/app/Models/Project.php index dc43be0e..cd289287 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -9,4 +9,5 @@ class Project extends Model { use HasFactory, SoftDeletes; + protected $fillable = ['name']; } diff --git a/app/Models/User.php b/app/Models/User.php index e23e0905..3903b807 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -4,6 +4,7 @@ use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; @@ -41,4 +42,9 @@ class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + + public function scopeActive(Builder $query): void + { + $query->whereNotNull('email_verified_at'); + } }