Skip to content

Commit

Permalink
Tasks Done
Browse files Browse the repository at this point in the history
  • Loading branch information
Sayouti1 committed Sep 14, 2023
1 parent d194f14 commit 8dcd176
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
7 changes: 5 additions & 2 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
use App\Models\Project;
use App\Models\Stat;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ProjectController extends Controller
{
public function store(Request $request)
{
// TASK: Currently this statement fails. Fix the underlying issue.
Project::create([
DB::table('projects')->insert([
'name' => $request->name
]);

Expand All @@ -26,6 +27,8 @@ public function mass_update(Request $request)
// where name = $request->old_name

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

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

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

return view('projects.index', compact('projects'));
}
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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');
}
Expand All @@ -68,7 +69,7 @@ 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();
$users = User::whereNotNull('email_verified_at')->get();//active()->get();

return view('users.index', compact('users'));
}
Expand Down
50 changes: 50 additions & 0 deletions app/Observers/StatObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Observers;

use App\Models\Stat;
use Illuminate\Support\Facades\DB;

class StatObserver
{
/**
* Handle the Stat "created" event.
*/
public function created(Stat $stat): void
{
//
DB::table('stats')->increment('projects_count');
}

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

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

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

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

namespace App\Providers;

use App\Models\Stat;
use App\Observers\StatObserver;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
Expand All @@ -28,5 +30,6 @@ class EventServiceProvider extends ServiceProvider
public function boot()
{
//
Stat::observe(StatObserver::class);
}
}

0 comments on commit 8dcd176

Please sign in to comment.