Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task 01 #421

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
11 changes: 7 additions & 4 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class ProjectController extends Controller
public function store(Request $request)
{
// TASK: Currently this statement fails. Fix the underlying issue.
Project::create([
'name' => $request->name
]);
$project = new Project();
$project->name = $request->name;
$project->save();

return redirect('/')->with('success', 'Project created');
}
Expand All @@ -26,6 +26,9 @@ public function mass_update(Request $request)
// where name = $request->old_name

// Insert Eloquent statement below
$project = Project::where('name', $request->old_name)->first();
$project->name = $request->new_name;
$project->save();

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::withTrashed()->get();

return view('projects.index', compact('projects'));
}
Expand Down
30 changes: 25 additions & 5 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class UserController extends Controller
{
Expand All @@ -15,23 +16,37 @@ public function index()
// order by created_at desc
// limit 3

$users = User::all(); // replace this with Eloquent statement
$users = User::whereNotNull('email_verified_at') // replace this with Eloquent statement
->orderByDesc('created_at')
->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
$user = User::where('name', $name)
->where('email', $email)
->first();

// if not found, create a user with $name, $email and random password
$user = NULL;
if (!$user) {
$user = User::create([
'name' => $name,
'email' => $email,
'password' => bcrypt(Str::random(10)),
]);
}

return view('users.show', compact('user'));
}
Expand All @@ -40,7 +55,11 @@ 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::firstOrCreate(
['name' => $name, 'email' => $email],
['name' => $name, 'email' => $email, 'password' => Str::random(10)]
);


return view('users.show', compact('user'));
}
Expand All @@ -52,6 +71,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 All @@ -60,7 +80,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();

return view('users.index', compact('users'));
}
Expand Down
3 changes: 3 additions & 0 deletions app/Models/Morningnews.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ class Morningnews extends Model
use HasFactory;

protected $fillable = ['title', 'news_text'];

// defining the correct table name
protected $table = 'morning_news';
}
50 changes: 50 additions & 0 deletions app/Observers/ProjectObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Observers;

use App\Models\Project;
use App\Models\Stat;

class ProjectObserver
{
/**
* Handle the Project "created" event.
*/
public function created(Project $project): void
{
// TASK: update stats set projects_count = projects_count + 1
Stat::where('id', 1)->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
{
//
}
}
5 changes: 4 additions & 1 deletion 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\Project;
use App\Observers\ProjectObserver;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
Expand All @@ -20,13 +22,14 @@ class EventServiceProvider extends ServiceProvider
],
];


/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
//
Project::observe(ProjectObserver::class);
}
}
Loading
Loading