diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 5877c736..551f9d49 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -11,6 +11,9 @@ class ProjectController extends Controller public function store(Request $request) { // TASK: Currently this statement fails. Fix the underlying issue. + $request->validate([ + 'name' => 'required|string', + ]); Project::create([ 'name' => $request->name ]); @@ -26,7 +29,8 @@ 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,7 +39,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..d40e5d91 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -1,6 +1,7 @@ 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 = NULL; // TASK: find user by $userId or show "404 not found" page + $user = User::findOrFail($userId); return view('users.show', compact('user')); } @@ -31,8 +34,16 @@ 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 = NULL; + $user = User::firstOrCreate( + [ + 'name' => $name, + 'email' => $email, + ], + [ + 'password' => bcrypt(Str::random(10)), + ] + ); return view('users.show', compact('user')); } @@ -40,7 +51,14 @@ 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 = NULL; // updated or created user + $user = User::updateOrCreate( + ['name' => $name], // Search criteria + [ + 'email' => $email, + 'password' => bcrypt(Str::random(10)) // Set password if creating new user + ] + ); return view('users.show', compact('user')); } @@ -52,6 +70,7 @@ public function destroy(Request $request) // $request->users is an array of IDs, ex. [1, 2, 3] // Insert Eloquent statement here + User::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..b925fd16 100644 --- a/app/Models/Morningnews.php +++ b/app/Models/Morningnews.php @@ -9,5 +9,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..ee14b9ea 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -2,11 +2,16 @@ namespace App\Models; +use App\Observers\ProjectObserver; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Database\Eloquent\Attributes\ObservedBy; class Project extends Model { use HasFactory, SoftDeletes; + protected $fillable = [ + 'name' + ]; } diff --git a/app/Models/User.php b/app/Models/User.php index e23e0905..57c6dc4f 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -3,6 +3,7 @@ namespace App\Models; use Illuminate\Contracts\Auth\MustVerifyEmail; +use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -41,4 +42,9 @@ class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + + public function scopeActive(Builder $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..b43a8309 --- /dev/null +++ b/app/Observers/ProjectObserver.php @@ -0,0 +1,50 @@ +projects_count += 1; + $stats->save(); + } + + /** + * Handle the Project "updated" event. + */ + public function updated(Project $project): void + { + // + } + + /** + * Handle the Project "deleted" event. + */ + public function deleted(Project $project): void + { + // + } + + /** + * Handle the Project "restored" event. + */ + public function restored(Project $project): void + { + // + } + + /** + * Handle the Project "force deleted" event. + */ + public function forceDeleted(Project $project): void + { + // + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ee8ca5bc..5d149b6d 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,6 @@ public function register() */ public function boot() { - // + Project::observe(ProjectObserver::class); } }