From ccd628964b76d970be7a95e08cb2b31537cf4cdd Mon Sep 17 00:00:00 2001 From: feras Date: Mon, 26 Aug 2024 09:52:04 +0300 Subject: [PATCH] slove eloquent --- app/Http/Controllers/ProjectController.php | 4 +- app/Http/Controllers/UserController.php | 31 +++++++++++-- app/Models/Morningnews.php | 2 + app/Models/Project.php | 4 ++ app/Models/User.php | 5 ++ app/Observers/ProjectObserver.php | 53 ++++++++++++++++++++++ app/Providers/AppServiceProvider.php | 4 +- 7 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 app/Observers/ProjectObserver.php diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 5877c736..7bd17d39 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -5,6 +5,7 @@ use App\Models\Project; use App\Models\Stat; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; class ProjectController extends Controller { @@ -26,6 +27,7 @@ public function mass_update(Request $request) // where name = $request->old_name // Insert Eloquent statement below + DB::update('update projects set name = ? where name = ?', [$request->new_name, $request->old_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')); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 9b8c73dc..fd3847dc 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\DB; +use Str; class UserController extends Controller { @@ -14,15 +16,16 @@ public function index() // where email_verified_at is not null // order by created_at desc // limit 3 + $users = DB::select('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::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 = User::findOrFail($userId); // TASK: find user by $userId or show "404 not found" page return view('users.show', compact('user')); } @@ -31,16 +34,34 @@ 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('email', $email)->where('name', $name)->first(); + + if (!$user) { + $user = new User(); + $user->name = $name; + $user->email = $email; + $user->password = bcrypt(Str::random(10)); // Generate a random 10-character password + $user->save(); + } 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 = NULL; // updated or created user + $user = User::where('email', $email)->where('name', $name)->first(); + + if (!$user) { + $user = new User(); + $user->name = $name; + $user->email = $email; + $user->password = bcrypt(Str::random(10)); // Generate a random 10-character password + $user->save(); + } return view('users.show', compact('user')); } @@ -52,6 +73,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'); } @@ -64,5 +86,4 @@ public function only_active() return view('users.index', compact('users')); } - } diff --git a/app/Models/Morningnews.php b/app/Models/Morningnews.php index 2335e17d..48c8e2de 100644 --- a/app/Models/Morningnews.php +++ b/app/Models/Morningnews.php @@ -9,5 +9,7 @@ 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..140ce2e1 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..9eed569e 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('email_verified_at', 'active'); + } } diff --git a/app/Observers/ProjectObserver.php b/app/Observers/ProjectObserver.php new file mode 100644 index 00000000..88cac312 --- /dev/null +++ b/app/Observers/ProjectObserver.php @@ -0,0 +1,53 @@ +increment('projects_count'); + } + public function created(Project $project): void + { + // + } + + /** + * 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); } }