diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 5877c736..505534f2 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -14,7 +14,6 @@ public function store(Request $request) Project::create([ 'name' => $request->name ]); - return redirect('/')->with('success', 'Project created'); } @@ -27,6 +26,8 @@ public function mass_update(Request $request) // Insert Eloquent statement below + Project::where('name', $request->old_name)->update(['name' => $request->new_name]); + return redirect('/')->with('success', 'Projects updated'); } @@ -35,7 +36,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')); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 9b8c73dc..668ba4d8 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -4,26 +4,24 @@ use App\Models\User; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Str; class UserController extends Controller { public function index() { - // TASK: turn this SQL query into Eloquent - // select * from users - // where email_verified_at is not null - // 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(); 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); return view('users.show', compact('user')); } @@ -31,16 +29,42 @@ 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::where('name', $name) + ->where('email', $email) + ->first(); + + if (!$user) { + $password = Str::random(8); + + $user = User::create([ + 'name' => $name, + 'email' => $email, + 'password' => bcrypt($password), // Hash the password using bcrypt + ]); + } return view('users.show', compact('user')); } + 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::where('name', $name)->first(); + + if ($user) { + $user->update([ + 'email' => $email, + ]); + } else { + $password = Str::random(8); + + $user = User::create([ + 'name' => $name, + 'email' => $email, + 'password' => Hash::make($password), + ]); + } return view('users.show', compact('user')); } @@ -52,6 +76,7 @@ public function destroy(Request $request) // $request->users is an array of IDs, ex. [1, 2, 3] // Insert Eloquent statement here + $user = User::destroy($request->users); return redirect('/')->with('success', 'Users deleted'); } @@ -60,7 +85,7 @@ public function only_active() { // TASK: That "active()" doesn't exist at the moment. // Create this scope to filter "where email_verified_at is not null" - $users = User::active()->get(); + $users = User::active()->get();; return view('users.index', compact('users')); } diff --git a/app/Models/Morningnews.php b/app/Models/Morningnews.php index 2335e17d..baf462b9 100644 --- a/app/Models/Morningnews.php +++ b/app/Models/Morningnews.php @@ -7,6 +7,7 @@ class Morningnews extends Model { + protected $table = 'morning_news'; use HasFactory; protected $fillable = ['title', 'news_text']; diff --git a/app/Models/Project.php b/app/Models/Project.php index dc43be0e..6f5b54a2 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -8,5 +8,6 @@ class Project extends Model { + protected $fillable = ['name']; use HasFactory, SoftDeletes; } diff --git a/app/Models/User.php b/app/Models/User.php index e23e0905..a02bf342 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -41,4 +41,9 @@ class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + + public function scopeActive($query) + { + return $query->where('active', true); + } } diff --git a/app/Observers/ProjectObserver.php b/app/Observers/ProjectObserver.php new file mode 100644 index 00000000..7c1fa097 --- /dev/null +++ b/app/Observers/ProjectObserver.php @@ -0,0 +1,18 @@ +save(); + // DB::update('update stats set projects_count = projects_count + 1'); + Stat::increment('projects_count'); + +} +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ee8ca5bc..10804ec4 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,8 @@ namespace App\Providers; +use App\Models\Project; +use App\Observers\ProjectObserver; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -23,6 +25,7 @@ public function register() */ public function boot() { - // + Project::observe(ProjectObserver::class); + } }