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 #417

Open
wants to merge 2 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
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_KEY=base64:FT581GI+XXyBTbxZprqEJ0VD9tbMxUx5LCiOwAs8050=
APP_DEBUG=true
APP_URL=http://localhost

Expand Down
6 changes: 4 additions & 2 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ 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,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()->all();

return view('projects.index', compact('projects'));
}
Expand All @@ -50,5 +53,4 @@ public function store_with_stats(Request $request)

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

}
30 changes: 24 additions & 6 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ public function index()
// order by created_at desc
// limit 3

$users = User::all(); // replace this with Eloquent statement
$users = User::query()
->where('email_verified_at', '!=', null)
->orderBy('created_at', 'desc')
->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
$user = User::findOrFail($userId); // TASK: find user by $userId or show "404 not found" page

return view('users.show', compact('user'));
}
Expand All @@ -31,7 +35,16 @@ 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::query()
->where('email', $email)
->where('name', $name)
->firstOrCreate(
[
'email' => $email,
'name' => $name,
'password' => 'ewa'
]
);

return view('users.show', compact('user'));
}
Expand All @@ -40,7 +53,13 @@ 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::query()
->where('name', $name)
->updateOrCreate([
'name' => $name,
'email' => $email,
'password' => 'ewa'
]); // updated or created user

return view('users.show', compact('user'));
}
Expand All @@ -51,7 +70,7 @@ public function destroy(Request $request)
// SQL: delete from users where id in ($request->users)
// $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 @@ -64,5 +83,4 @@ public function only_active()

return view('users.index', compact('users'));
}

}
1 change: 1 addition & 0 deletions app/Models/Morningnews.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ class Morningnews extends Model
{
use HasFactory;

protected $table = 'morning_news';
protected $fillable = ['title', 'news_text'];
}
5 changes: 5 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

namespace App\Models;

use App\Observers\ProjectObserver;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

#[ObservedBy([ProjectObserver::class])]
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 @@ -3,6 +3,7 @@
namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
Expand Down Expand Up @@ -41,4 +42,9 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

public function scopeActive(Builder $query)
{
$query->where('email_verified_at', '!=', null);
}
}
48 changes: 48 additions & 0 deletions app/Observers/ProjectObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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 = Stat::first();
$stat->projects_count += 1;
$stat->save();
}

/**
* 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
{
//
}
}
Loading
Loading