diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 5877c736..cb4c63c8 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -8,10 +8,11 @@ class ProjectController extends Controller { + public function store(Request $request) { // TASK: Currently this statement fails. Fix the underlying issue. - Project::create([ + Project::create([ 'name' => $request->name ]); @@ -24,8 +25,9 @@ public function mass_update(Request $request) // update projects // set name = $request->new_name // 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 +37,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')); } @@ -48,6 +50,10 @@ public function store_with_stats(Request $request) $project->name = $request->name; $project->save(); + // $project = Project::create([ + // 'name' => $request->name + // )]; + return redirect('/')->with('success', 'Project created'); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 9b8c73dc..6b000fdc 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -4,6 +4,8 @@ use App\Models\User; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Str; class UserController extends Controller { @@ -13,25 +15,32 @@ public function index() // 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 + // limit 3 + $users = User::whereNotNull('email_verified_at') + ->orderByDesc('created_at') //latest('created_at') + ->limit(3) + ->get(); + // $users = User::all(); // 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 = NULL; // TASK: find user by $userId or show "404 not found" page + $user = User::findOrFail($userId); return view('users.show', compact('user')); } public function check_create($name, $email) { // TASK: find a user by $name and $email + $user = User::firstOrCreate( + ['name' => $name , 'email' => $email] , + ['password' => Hash::make(str::password())] , + ); // if not found, create a user with $name, $email and random password - $user = NULL; + return view('users.show', compact('user')); } @@ -39,8 +48,14 @@ public function check_create($name, $email) public function check_update($name, $email) { // TASK: find a user by $name and update it with $email + $user = User::firstOrNew( + ['name' => $name] , + ['email' => $email , 'password' => Hash::make(str::random())] , + ); + $user->email = $email; + $user->save(); // if not found, create a user with $name, $email and random password - $user = NULL; // updated or created user + // $user = NULL; // updated or created user return view('users.show', compact('user')); } @@ -52,7 +67,8 @@ 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'); } @@ -61,7 +77,6 @@ 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(); - return view('users.index', compact('users')); } 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..83524a11 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -9,4 +9,13 @@ class Project extends Model { use HasFactory, SoftDeletes; + + /** + * The attributes that are mass assignable. + * + * @var string[] + */ + protected $fillable= [ 'name' , + ]; + } diff --git a/app/Models/User.php b/app/Models/User.php index e23e0905..8dd30d6c 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; @@ -12,6 +13,7 @@ class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable; + /** * The attributes that are mass assignable. * @@ -41,4 +43,12 @@ class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + + /** + * Scope a query. + */ + public function scopeActive(Builder $query): void + { + $query->whereNotNull('email_verified_at'); + } } diff --git a/app/Observers/ProjectObserver.php b/app/Observers/ProjectObserver.php new file mode 100644 index 00000000..a31377e6 --- /dev/null +++ b/app/Observers/ProjectObserver.php @@ -0,0 +1,49 @@ +increment('projects_count'); + } + + /** + * 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 "forceDeleted" event. + */ + public function forceDeleted(Project $project): void + { + // ... + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index a9f10a63..006d189c 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -7,6 +7,9 @@ use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Support\Facades\Event; +use App\Models\Project; +use App\Observers\ProjectObserver; + class EventServiceProvider extends ServiceProvider { /** @@ -27,6 +30,6 @@ class EventServiceProvider extends ServiceProvider */ public function boot() { - // + Project::observe(ProjectObserver::class); } }