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

my solution #394

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ 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,9 +36,11 @@ public function destroy($projectId)
Project::destroy($projectId);

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

return view('projects.index', compact('projects'));

}

public function store_with_stats(Request $request)
Expand All @@ -47,8 +50,8 @@ public function store_with_stats(Request $request)
$project = new Project();
$project->name = $request->name;
$project->save();

return redirect('/')->with('success', 'Project created');
}

}

37 changes: 30 additions & 7 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

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

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class UserController extends Controller
{
public function index()
Expand All @@ -14,16 +16,19 @@ public function index()
// where email_verified_at is not null
// order by created_at desc
// limit 3
$users =User::whereNotNull('email_verified_at')->orderBy('created_at','desc')->limit(3)->get();

$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::find($userId);
if(is_null($user)){
abort(404);
}
return view('users.show', compact('user'));
}

Expand All @@ -32,16 +37,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::firstOrCreate(
['name' => $name, 'email' => $email],
['password' => Hash::make(Str::random(10))]
);
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 = DB::table('users')->updateOrInsert(['name' => $name, 'email' => $email]);
$user = User::where('name', $name)->first();

if ($user) {
// If user is found, update the email
$user->email = $email;
$user->save();
} else {
// If user is not found, create a new user with a random password
$user = User::create([
'name' => $name,
'email' => $email,
'password' => Hash::make(Str::random(10)),
]);
}
return view('users.show', compact('user'));
}

Expand All @@ -52,7 +75,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 Down
1 change: 1 addition & 0 deletions app/Models/Morningnews.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class Morningnews extends Model
{
use HasFactory;
protected $table = 'morning_news';

protected $fillable = ['title', 'news_text'];
}
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'];
protected $dates = ['deleted_at'];
}
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 created(Project $project): void
{
Stat::query()->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
{
//
}
}
4 changes: 3 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Models\Project;
use App\Observers\ProjectObserver;

class AppServiceProvider extends ServiceProvider
{
Expand All @@ -23,6 +25,6 @@ public function register()
*/
public function boot()
{
//
Project::observe(ProjectObserver::class);
}
}
Loading