From b67cca339cfe49bbef9c60381475be177bfe5731 Mon Sep 17 00:00:00 2001 From: Anas Ouda Date: Thu, 9 Jan 2025 10:58:08 +0200 Subject: [PATCH] Solve Eloquent basics test --- app/Http/Controllers/ProjectController.php | 6 ++- app/Http/Controllers/UserController.php | 24 +++++++++-- app/Models/Morningnews.php | 2 + app/Models/Project.php | 2 + app/Models/User.php | 11 +++++ app/Observers/ProjectObserver.php | 49 ++++++++++++++++++++++ app/Providers/AppServiceProvider.php | 4 +- 7 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 app/Observers/ProjectObserver.php diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 5877c736..daebc7f6 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -20,12 +20,16 @@ public function store(Request $request) public function mass_update(Request $request) { + + // TASK: Transform this SQL query into Eloquent // 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 +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..e941de06 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\Str; class UserController extends Controller { @@ -15,23 +16,32 @@ public function index() // order by created_at desc // limit 3 - $users = User::all(); // replace this with Eloquent statement + //$users = User::all(); // replace this with Eloquent statement + + $users=User::where('email_verified_at','!=','null')->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 + // 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 // if not found, create a user with $name, $email and random password - $user = NULL; + $user = User::firstOrCreate( + ['name' => $name, 'email' => $email], + ['password' => bcrypt(Str::password())] + );; return view('users.show', compact('user')); } @@ -40,7 +50,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 + + // updated or created user + $user =User::updateOrCreate( + ['name' => $name], + ['email' => $email, 'password' => bcrypt(Str::password())] + ); return view('users.show', compact('user')); } @@ -51,6 +66,7 @@ public function destroy(Request $request) // SQL: delete from users where id in ($request->users) // $request->users is an array of IDs, ex. [1, 2, 3] + User::destroy($request->users); // Insert Eloquent statement here return redirect('/')->with('success', 'Users deleted'); diff --git a/app/Models/Morningnews.php b/app/Models/Morningnews.php index 2335e17d..4acc77de 100644 --- a/app/Models/Morningnews.php +++ b/app/Models/Morningnews.php @@ -10,4 +10,6 @@ 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..979d5b81 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -9,4 +9,6 @@ class Project extends Model { use HasFactory, SoftDeletes; + + protected $fillable = ['name']; } diff --git a/app/Models/User.php b/app/Models/User.php index e23e0905..f29654c5 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\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -12,6 +13,10 @@ class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable; + + + + /** * The attributes that are mass assignable. * @@ -41,4 +46,10 @@ class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + + + public function scopeActive(Builder $query){ + + $query->where('email_verified_at','!=','null'); + } } diff --git a/app/Observers/ProjectObserver.php b/app/Observers/ProjectObserver.php new file mode 100644 index 00000000..600563fd --- /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 "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); } }