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

Complete #339

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
6 changes: 3 additions & 3 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function mass_update(Request $request)
// 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');
}

Expand All @@ -35,8 +35,8 @@ 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
19 changes: 12 additions & 7 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,25 @@ public function index()
// order by created_at desc
// limit 3

$users = User::all(); // replace this with Eloquent statement

$users = User::whereNotNull('email_verified_at')->orderBy('created_at', 'desc')->take(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
// TASK4: find a user by $name and $email
// if not found, create a user with $name, $email and random password
$user = NULL;
$user = User::firstOrNew(['email' => $email], ['name' => $name]);
$user->password = rand(8,12);
$user->save();

return view('users.show', compact('user'));
}
Expand All @@ -40,7 +42,8 @@ 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]); // updated or created user

return view('users.show', compact('user'));
}
Expand All @@ -53,6 +56,8 @@ public function destroy(Request $request)

// Insert Eloquent statement here

User::destroy($request->users);

return redirect('/')->with('success', 'Users deleted');
}

Expand Down
2 changes: 1 addition & 1 deletion app/Models/Morningnews.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
class Morningnews extends Model
{
use HasFactory;

protected $table = 'morning_news';
protected $fillable = ['title', 'news_text'];
}
3 changes: 3 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
class Project extends Model
{
use HasFactory, SoftDeletes;
protected $fillable = [
'name'
];
}
4 changes: 4 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];
public function scopeActive($query)
{
return $query->whereNotNull('email_verified_at');
}
}
49 changes: 49 additions & 0 deletions app/Observers/ProjectObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Observers;

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

class ProjectObserver
{
/**
* Handle the Project "created" event.
*/
public function creating(Project $project): void
{
Stat::increment('projects_count', 1);
}

/**
* 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
{
//
}
}
3 changes: 3 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use App\Models\Project;
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function up()
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('password')->default(rand(0,3));
$table->rememberToken();
$table->timestamps();
});
Expand Down
Loading