diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 5877c736..efcb4fb5 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'); } @@ -35,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')); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 9b8c73dc..8dc56304 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')); } @@ -40,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')); } @@ -52,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'); } 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; 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' + ]; } 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'); + } } 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 @@ +