Skip to content

Commit

Permalink
solve eloquent test
Browse files Browse the repository at this point in the history
  • Loading branch information
ahastarin committed Dec 10, 2023
1 parent 1eb80e6 commit 1385c1f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 6 deletions.
8 changes: 7 additions & 1 deletion app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function mass_update(Request $request)
// where name = $request->old_name

// Insert Eloquent statement below
User::where('name', $request->old_name)
->update([
'name' => $request->new_name
]);

return redirect('/')->with('success', 'Projects updated');
}
Expand All @@ -35,7 +39,9 @@ public function destroy($projectId)
Project::destroy($projectId);

// TASK: change this Eloquent statement to include the soft-deletes records
$projects = Project::all();
$projects = Project::withTrashed()
->where('id', $projectId)
->restore();;

return view('projects.index', compact('projects'));
}
Expand Down
15 changes: 10 additions & 5 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ public function index()
// order by created_at desc
// limit 3

$users = User::all(); // replace this with Eloquent statement
$users = User::whereNot('email_verified_at', NULL)
->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'));
}
Expand All @@ -31,16 +34,17 @@ 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::firstOrNew(['name' => $name, 'email' => $email]);
$user->password(Hash::make(Str::rand(8)));
$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 = User::UpdateOrCreate(['name' => 'name'], ['email' => $email, 'password' => Hash::make(Str::rand(8))]); // updated or created user

return view('users.show', compact('user'));
}
Expand All @@ -52,6 +56,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');
}
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
class Project extends Model
{
use HasFactory, SoftDeletes;

protected $fillable = ['name']
}
6 changes: 6 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

public function scopeActive(Builder $query): void
{
$query->whereNot('email_verified_at', NULL);
}

}
52 changes: 52 additions & 0 deletions app/Observers/ProjectObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Observers;

use App\Models\Stat;
use App\Models\User;

class ProjectObserver
{
/**
* Handle the User "created" event.
*/
public function created(User $user): void
{
// ...
$stat = new Stat();
$stat->projects_count+1;
$stat->save();
}

/**
* Handle the User "updated" event.
*/
public function updated(User $user): void
{
// ...
}

/**
* Handle the User "deleted" event.
*/
public function deleted(User $user): void
{
// ...
}

/**
* Handle the User "restored" event.
*/
public function restored(User $user): void
{
// ...
}

/**
* Handle the User "forceDeleted" event.
*/
public function forceDeleted(User $user): void
{
// ...
}
}
3 changes: 3 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace App\Providers;

use App\Models\Project;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use App\Observers\ProjectObserver;

class EventServiceProvider extends ServiceProvider
{
Expand All @@ -28,5 +30,6 @@ class EventServiceProvider extends ServiceProvider
public function boot()
{
//
Project::observe(ProjectObserver::class);
}
}

0 comments on commit 1385c1f

Please sign in to comment.